Recent Changes - Search:

Web Design

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

Server-side Processing: Introduction to PHP Programming

Overview: This tutorial introduces PHP programming as the basic of server-side processing for Web development.


The Environment

  • To begin using PHP, you need to have a proper development and running environment set up.
  • PHP is usually used in combination with a Web server.
  • Requests for running PHP programs are received by the Web server, and are handled by the PHP interpreter.
  • The results obtained after execution are returned to the Web server, which takes care of transmitting them to the client browser.

Activity 1: Install XAMPP as a Web server in your PC


Activity 2: Test a simple PHP program running in your Web server

  • Creat a new folder under the document root of the Web server, say C:\xampp\htdocs\test-php\
  • In the new folder you just created, creat a new file with the following content, say say C:\xampp\htdocs\test-php\test.php
      <html>
      <head></head>
      <body>

      Agent: So who do you think you are, anyhow?
      <br />

      <?php
      // print output
      echo 'Neo: I am Neo, but my people call me The One.';
      ?>

      </body>
      </html>
  • Use your browser to access the PHP program.
  • Examine the HTML code received by the browser.
Q1) What your Web browser will receive from the server-side program?
Q2) Can we use a Web browser to get the source code of the server-side program?

PHP Syntax

  • PHP lets you embed PHP code in regular HTML pages, and execute the embedded PHP code when the page is requested.
  • These embedded PHP code is enclosed within special start and end tags:
      <?php

      ... PHP code ...

      ?>
  • For example,
      <html>
      <head>
      <title>My First PHP Page</title>
      </head>
      <body>
      <?php
        echo "Hello World! ";

        echo "Hello World! ";
        echo "Hello World! ";
        echo "Hello World! ";        echo "Hello World! ";
      ?>
      </body>
      </html>
  • The semicolon signifies the end of a PHP statement.
  • Whitespace is ignored between PHP statements.
  • PHP comments
    • Single Line Comments, start with //
    • Multiple Line Comments, start with /* and end with */
    (They are similar to comments of other programming languages.)

PHP variables

  • A variable is a means of storing a value.
  • A variable is defined with the following form:
      $variable_name = Value;
  • Boolean variable specifies a true or false value.
      $found = true;
  • Integer variable
      $age = 34;
  • Floating-point variable specifies a fractional number
      $temperature = 21.89
  • String variable
      $car = 'BMW';
(Question: What is the major difference when it compares to other programming language you have learned?)

PHP operators

  • Assignment Operators
      $my_var = 4;
      $another_var = $my_var;
  • Arithmetic Operators
      + 	Addition
      - 	Subtraction
      * 	Multiplication
      / 	Division
      % 	Modulus
  • Relational operators / Comparison Operators
      == 	Equal To
      != 	Not Equal To
      < 	Less Than
      > 	Greater Than
      <= 	Less Than or Equal To
      >= 	Greater Than or Equal To
  • String Operators: the period "." is used to add two strings together
      $a = 'This is';
      $b = 'funny';
      $c = $a.' '.$b;

PHP's arrays

  • An array is a complex variable that allows you to store multiple values in a single variable
  • An array is handy when you need to store and represent related information.
  • Example 1:
      $pizzaToppings = array('onion', 'tomato', 'cheese', 'anchovies', 'ham');
  • Example 2:
      $fruits = array('red' => 'apple', 'yellow' => 'banana', 'purple' => 'plum');
  • Example 3:
      $pasta[0] = 'spaghetti';
      $pasta[1] = 'penne';
      $pasta[2] = 'macaroni';
  • Example 4:
      $menu['breakfast'] = 'bacon and eggs';
      $menu['lunch'] = 'roast beef';
      $menu['dinner'] = 'lasagna'; 

PHP's control statements

  • If Statement
      $name = "steven";
      if ( $name == "steven" ) {
        echo "Your name is Steven!<br />";
      }
      echo "Welcome to my homepage!";
  • If/Else statement
      if ( $number >= 3 ) {
        echo "The if statement evaluated to true";
      } else {
        echo "The if statement evaluated to false";
      }
  • For loop
      for ( initialize a counter; conditional statement; increment a counter){
        // do this code;
      }

      // Example
      for ( $counter = 10; $counter <= 100; $counter += 10) {
        // do something based on the counter;
      }
  • Foreach
      $pizzaToppings = array('onion', 'tomato', 'cheese', 'anchovies', 'ham');
      echo '<ol>';
      foreach ($pizzaToppings as $topping) {
          echo '<li>'.$topping.'</li>';
      }
      echo '</ol>';

Define and call functions

  • Create a function
      // define a function
      function getCircumference($radius) {
          $result = $radius*2*3.14;
          return $result;
      }
  • Call a function
      // call a function with an argument
      $the_radius = 10;
      $the_circumference = getCircumference($the_radius);

Activity 3: PHP For loop

  • Write a PHP program that outputs 12x12 times table like the following:
  • You can use HTML table to present the result:
    <table border="1" cellpadding="5">
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    <tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
    </table>
Q3) Submit your PHP program for this activity.

Activity 4: PHP Control Statements

  • Write a PHP program that outputs the distribution of prime numbers in the following way:
Hint 1: The following PHP code is useful for you; it outputs all the prime numbers between 1 and 100.
      <?php
        for ($i = 1; $i <= 100; $i++)
        {
          if($i % 2 != 1) continue;
          $d = 3;
          $x = sqrt($i);
          while ($i % $d != 0 && $d < $x) $d += 2;
          if((($i % $d == 0 && $i != $d) * 1) == 0) echo $i.' ';
        }
      ?>
Hint 2: You can use the HTML tag <strong> and </strong> to bold a number.
Q4) Submit your PHP program for this activity.

References:


Submission

  • Please submit your work to Steven by email: sochoy@ouhk.edu.hk.
  • Please use SXXXXXXX - WD Tutorial 18 Submission as the email subject.

Edit - History - Print - Recent Changes - Search
Page last modified on February 22, 2011, at 09:20 AM