PHP Preprocessor Hypertext programming language

PHP Preprocessor Hypertext programming language

What is PHP (Preprocessor Hypertext programming language)

The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications. This tutorial helps you to build your base with PHP

<!DOCTYPE html>

echo “My first PHP script!”;



My first PHP script!

Applications of PHP

As mentioned before, PHP is one of the most widely used language over the web. I’m going to list few of them here:

  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
  • You add, delete, modify elements within your database through PHP.
  • Access cookies variables and set cookies.
  • Using PHP, you can restrict users to access some pages of your website.
  • It can encrypt data.


This PHP tutorial is designed for PHP programmers who are completely unaware of PHP concepts but they have basic understanding on computer programming.

Create an Opinion Poll Application

Step 1

CREATE TABLE `js_libraries` (


  `choice` tinyint(4) NOT NULL DEFAULT '0',

  `ts` timestamp NULL DEFAULT NULL,

  PRIMARY KEY (`id`)


Step 2


class Opinion_poll_model {

 private $db_handle; private $host = 'localhost'; private $db = 'opinion_poll';private $uid = 'root'; private $pwd = 'melody';

    public function __construct() {

        $this->db_handle = mysqli_connect($this->host, $this->uid, $this->pwd); //connect to MySQL server

        if (!$this->db_handle) die("Unable to connect to MySQL: " . mysqli_error());

        if (!mysqli_select_db($this->db_handle,$this->db)) die("Unable to select database: " . mysqli_error());


    private function execute_query($sql_stmt) {

        $result = mysqli_query($db_handle,$sql_stmt); //execute SQL statement

        return !$result ? FALSE : TRUE;


    public function select($sql_stmt) {

        $result = mysqli_query($db_handle,$sql_stmt);

        if (!$result) die("Database access failed: " . mysqli_error());

        $rows = mysqli_num_rows($result);

        $data = array();

        if ($rows) {

            while ($row = mysqli_fetch_array($result)) {

                $data = $row;



        return $data;


    public function insert($sql_stmt) {

        return $this->execute_query($sql_stmt);


    public function __destruct(){






  • “public function __construct()” is the class constructor method that is used to establish the database connection
  • “public function execute_query(…)” is the method for executing queries such as insert, update and delete
  • “public function select” is the method for retrieving data from the database and returning a numeric array.
  • “public function insert(…)” is the insert method that calls the execute_query method.
  • “public function __destruct()” is the class destructor that closes the database connection.

Let’s now create the front controller index.php


require 'opinion_poll_model.php';

$model = new Opinion_poll_model();

if (count($_POST) == 1) {

    echo "<script>alert('You did not vote!');</script>";


if (count($_POST) > 1) {

    $ts = date("Y-m-d H:i:s");

    $option = $_POST['vote'][0];

    $sql_stmt = "INSERT INTO js_libraries (`choice`,`ts`) VALUES ($option,'$ts')";


    $sql_stmt = "SELECT COUNT(choice) choices_count FROM js_libraries;";

    $choices_count = $model->select($sql_stmt);

    $libraries = array("", "JQuery", "MooTools", "YUI Library", "Glow");

    $table_rows = '';

    for ($i = 1; $i < 5; $i++) {

        $sql_stmt = "SELECT COUNT(choice) choices_count FROM js_libraries WHERE choice = $i;";

        $result = $model->select($sql_stmt);

        $table_rows .= "<tr><td>" . $ libraries [$i] . " Got:</td><td><b>" . $result[0] . "</b> votes</td></tr>";


    require 'results.html.php';



require 'opinion.html.php';



  • “require ‘opinion_poll_model.php’;” loads the business logic class
  • “$model = new Opinion_poll_model();” creates an instance of the business logic class
  • “if (count($_POST) == 1)…” performs the data validation and uses JavaScript to display a message box if not candidate has been voted for.
  • “if (count($_POST) > 1)…” checks if a vote has been selected by counting the number of items in the $_POST array. If no item has been select, the $_POST will only contain the submit item. If a candidate has been chosen, the $_POST array will two elements, the submit and vote item. This code is also used to insert a new vote record and then display the results page
  • “exit;” is used to terminate the script execution after the results have been displayed so that the opinion poll form is not displayed.
  • “require ‘opinion.html.php’;” displays the opinion poll form if nothing has been selected.

Let’s now create the views. opinion.html.php



        <title>JavaScript Libraries - Opinion Poll</title>



        <h2>JavaScript Libraries - Opinion Poll</h2>

        <p><b>What is your favorite JavaScript?</b></p>

        <form method="POST" action="index.php">

            <p> <input type="radio" name="vote" value="1" />JQuery

                <br /><input type="radio" name="vote" value="2" />MooToolsl

                <br /><input type="radio" name="vote" value="3" />YUI Library

                <br /><input type="radio" name="vote" value="4" />Glow


            <p><input type="submit" name="submitbutton" value="OK" /></p>







        <title>JavaScript Libraries Poll Results</title>



        <h2>Opinion Poll Results</h2>

        <p><b>What is your favorite JavaScript Library?</b></p>

        <p><b><?php echo $choices_count[0]; ?></b> people have thus far taken part in this poll:</p>


            <?php echo($table_rows); ?>


Leave a Reply