Table of Contents

~~SLIDESHOW~~

Introduction to PHP (Part 1)

Supplementary Material

Provided for Reference. This material is no longer taught on this module.

Lecturer: Dr Chris P. Jobling.

An introduction to PHP, one of the most popular server-side scripting languages for web applications.

Introduction to PHP (Part 1)

PHP is rapidly becoming the favourite language for server-side web applications development. In this lecture we introduce the language and some of its features. To set PHP in context, you should study this lecture in conjunction with the Introduction to JavaScript.


Based on Chapter 12 of Robert W. Sebasta, Programming the World-Wide Web, 3rd Edition, Addison Wesley, 2006. and Chapter 12 of Chris Bates, Web Programming: Building Internet Applications, 3rd Edition, John Wiley, 2006.

Contents of this Lecture

Introduction to PHP with examples

The Examples for PHP Part 1.

Learning Outcomes

At the end of this lecture you should be able to answer these questions:

  1. How does a web server determine whether a requested document includes PHP code
  2. What are the two modes of the PHP processor?
  3. What are the syntax and semantics of the include construct?
  4. Which parts of PHP are case sensitive and which are not?
  5. What are the four scalar types of PHP?

Learning Outcomes (continued)

At the end of this lecture you should be able to answer these questions:

  1. How can a variable be tested to determine whether it is bound
  2. How can you specify to the PHP processor that you want uses of unbound variables to be reported
  3. How many bytes are used to store a character in PHP?
  4. What are the differences between single- and double-quoted literal strings

Learning Outcomes (continued)

At the end of this lecture you should be able to answer these questions:

  1. If an integer expression appears in Boolean context, how is its Boolean value determined?
  2. What happens when an integer arithmetic operation results in a value that cannot be represented as an integer?
  3. If a variable stores a string, how can the character at a specific position in that string be referenced?
  4. What does the chop function do?
  5. What is a coercion?

Learning Outcomes (continued)

At the end of this lecture you should be able to answer these questions:

  1. What are the three ways the value of a variable can be explicitly coerced to a specific type
  2. How can the type of a variable be determined?
  3. If a string is compared with a number, what happens?
  4. What is the advantage of using the unique closing reserved words such as endwhile?

Origins and Uses of PHP

Overview of PHP

General Syntactic Characteristics

General Syntactic Characteristics (continued)

    // JavaScript-like inline comment
    # perl-like inline comment
    /* Java-like block comment */

explanation: a block is defined as code for which variables defined between braces (not function bodies), are local to the enclosing block. Java has this property but JavaScript and PHP do not.

Primitives, Operations, and Expressions

Variables

Primitive Types

Strings


<note> Characters are single bytes: This means that, unlike HTML, PHP does not natively support Unicode characters. There are a pair of functions utf8_encode() and utf8_decode() which provide some Unicode support. There are implications of this if you want to develop PHP applications accessible in countries which do not use the utf-8 character set. </note>

Booleans

Arithmetic Operators and Expressions

String Operations and Functions

Scalar Type Conversions

      gettype($total)  // may return "unknown"
      is_integer($total) // a predicate function

Output

       echo "whatever";   # Only one parameter
       echo("first <br />", $sum)  # More than one
       print "Welcome to my site!";  # Only one

More Output

<p>Hello <?=$name?>, welcome to my site. It's <?=date("D M j Y")?> today.</p>

Hello World (PHP)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title> Trivial php example </title>
  </head>
  <body>
    <?php
      print "Welcome to my Web site!";
    ?>
  </body>
</html> 

First PHP Example

A simple example to illustrate a PHP document: today.php (today.php @ localhost)


<!DOCTYPE html>
 
<!-- today.php - A simple example to illustrate a PHP document -->
<html lang="en">
  <head> 
    <meta charset="utf-8" />
    <title> today.php &ndash; A simple example to illustrate a PHP document  </title>
  </head>
  <body>
    <p>
      <?php
        print "<b>Welcome to my home page <br /> <br />";
        print "Today is:</b> ";
        print date("l, F jS");
        print "<br />";
      ?>
    </p>
  </body>
</html>

Result:

Result of executing PHP script today.php

Control Expressions

Selection statements

Looping constructs

Other statements

Alternative componund delimiters

    if (...) :
      ...
    endif;

Another Example

An example to illustrate loops and arithmetic: powers.php (powers.php @ localhost)


<!DOCTYPE html>
 
<!-- powers.php
     An example to illustrate loops and arithmetic
     -->
<html lang="en">
  <head> 
    <meta charset="utf-8">
    <title> powers.php &ndash An example to illustrate loops and arithmetic </title>
  </head>
  <body>
    <table border = "border">
      <caption> Powers table </caption>
      <tr>
        <th> Number </th>
        <th> Square Root </th>
        <th> Square </th>
        <th> Cube </th>
        <th> Quad </th>
      </tr>
      <?php
        for ($number = 10; $number <=20; $number++) {
          $root = sqrt($number);
          $square = pow($number, 2);
          $cube = pow($number, 3);
          $quad = pow($number, 4);
          print("<tr align = 'center'> <td> $number </td>");
          print("<td> $root </td> <td> $square </td>");
          print("<td> $cube </td> <td> $quad </td> </tr>");
        }
      ?>
    </table>
  </body>
</html>

Result of executing the powers.php script

Intermingling HTML and PHP

    <?php
      $a = 7;
      $b = 7;
      if ($a == $b) {
        $a = 3 * $a;
    ?>
    <br />
    At this point, $a and $b are
    equal <br />
    So, we change $a to three times $a
    <?php
      }
    ?> 

<note warning> this is not necessarily a good idea! Notice how the end brace for the conditional is separated from the loop body by several unrelated HTML tags. This produces fragile code – if the second set of PHP tags where removed, a syntax error would occur which might be difficult to track down. </note>

Summary of This Lecture

Introduction to PHP with examples

The Examples for PHP Part 1.

Learning Outcomes

At the end of this lecture you should be able to answer these questions:

  1. How Does a Web server determine whether a requested document includes PHP code
  2. What are the two modes of the PHP processor?
  3. What are the syntax and semantics of the include construct?
  4. Which parts of PHP are case sensitive and which are not?
  5. What are the four scalar types of PHP?

Learning Outcomes (continued)

At the end of this lecture you should be able to answer these questions:

  1. How can a variable be tested to determine whether it is bound
  2. How can you specify to the PHP processor that you want uses of unbound variables to be reported
  3. How many bytes are used to store a character in PHP?
  4. What are the differences between single- and double-quoted literal strings

Learning Outcomes (continued)

At the end of this lecture you should be able to answer these questions:

  1. If an integer expression appears in Boolean context, how is its Boolean value determined?
  2. What happens when an integer arithmetic operation results in a value that cannot be represented as an integer?
  3. If a variable stores a string, how can the character at a specific position in that string be referenced?
  4. What does the chop function do?
  5. What is a coercion?

Learning Outcomes (continued)

At the end of this lecture you should be able to answer these questions:

  1. What are the three ways the value of a variable can be explicitly coerced to a specific type
  2. How can the type of a variable be determined?
  3. If a string is compared with a number, what happens?
  4. What is the advantage of using the unique closing reserved words such as endwhile?

What's Next?

Introduction to PHP (Part 2)

Further features of the PHP language

Previous Lecture | home | Next Lecture