Recent Changes - Search:

Network Programming

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.

Introduction to PHP Programming

A Network Programming Lecture by Steven Choy

Overview: The Environment - Install XAMPP as a Web server in your PC - Test a simple PHP program running in your Web server - PHP Syntax - PHP variables - PHP operators - PHP's arrays - PHP's control statements - Define and call functions - Some PHP functions


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

Note: Another popular choice of software package for setting up Web server in Windows is WampServer. You are encouraged to try out both XAMPP and WamServer to see which one is suitable for you.


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.

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', 'pepperoni');
  • Example 2:
      $fruits = array('red' => 'apple', 'yellow' => 'banana', 'purple' => 'plum', 'green' => 'grape');
  • 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', 'pepperoni');
      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);

Some PHP functions

  • String Position - strpos
      $numberedString = "1234567890";
      $fivePos = strpos($numberedString, "5");
      echo "The position of 5 in our string was $fivePos";
  • String Replacement - str_replace
      $rawstring = "Welcome Birmingham parents. Your replaceme is a pleasure to have!";
      $malestr = str_replace("replaceme", "son", $rawstring);
      $femalestr = str_replace("replaceme", "daughter", $rawstring);
      echo "Son: ". $malestr . "<br />";
      echo "Daughter: ". $femalestr;
  • String Explode - explode
      $rawPhoneNumber = "800-555-5555"; 
      $phoneChunks = explode("-", $rawPhoneNumber);

      //Result: $phoneChunks[0] is 800, $phoneChunks[1] is 555, etc
  • String Implode - implode
      $pieces = array("Hello", "World,", "I", "am", "Here!");
      $gluedTogetherSpaces = implode(" ", $pieces);
      $gluedTogetherDashes = implode("-", $pieces);
  • The Timestamp - date
      echo date();
      echo date("m/d/y");
  • Supply a Timestamp - mktime
      $tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
      echo "Tomorrow is ".date("m/d/y", $tomorrow); 

Class Exercise

  • Write a PHP program to calculate how many seconds and days you have lived up to that moment.

References:

Thanks for Reading

If you would rather like to have this lecture note in printed format, please click the print action link in the top right corner.

If you find any problem in this lecture note, please feel free to tell Steven via steven@findaway.hk.

Edit - History - Print - Recent Changes - Search
Page last modified on January 09, 2012, at 11:19 AM