A Short Language Primer

This section introduces the basic syntax of PHP. If you’re familiar with high-level languages such as C, Java, JavaScript, or Perl, you’ll be at home with PHP. The current version of PHP is PHP 4, and some details we present here are specific to this version.

As discussed previously, PHP scripts are surrounded by the PHP start tag <?php and the end tag ?>. You’ll often see the start tag abbreviated as <?, but this conflicts with the emerging XHTML standard and should be avoided.

Statements in a script are terminated with a semicolon. Statements can be formatted for readability by including any amount of whitespace—such as space characters, tab characters, or blank lines—in a script.

Comments can be included in a PHP script using the following styles:

// One line comment
  
#  Another one line comment
  
/* A
   multiple line
   comment */

Data can be output with the statements print , echo , and printf . The first two are often interchangeable, but echo has an advantage in that it can take more than one argument. The printf statement is used for more complex output and is identical to that used in other programming languages such as C and scripting languages such as awk. Consider a few examples:

// These are the same
echo "This is output";
print "This is output";
  
// echo can output more than one argument
echo 123, "is a number";
  
// printf can be used to control formatting
// This outputs 3.14
printf("pi is %.2f
", 3.14159);

Variables are identified by the prefix dollar sign ($) and variable names are case sensitive. Variables are declared and given a type when they’re first used. For example, the following creates a variable $x of type integer:

$x = 4;

The type of a variable can change in a script. For example, the following is valid:

// $x is an integer
$x = 4;
  
// Now it is a string
$x = "Selina";

PHP has four scalar variable types: integer, Boolean, string, and float. There are two compound types: object and array. Compound types contain elements that are scalar variables, and their types can be mixed. The object type isn’t discussed in this chapter, but here are examples of the other five types:

// $x is an integer
$x = 4;
  
// $x is a float
$x = 3.142;
  
// $x is a string
$x = "Richmond";
  
// $x and $y are Boolean
$x = true;
$y = false;
  
// $x is an array of strings
$x = array("one", "two", "three", "four", "five");

Arrays can be accessed by their numeric index or associatively. Index elements are numbered from zero. Consider two example arrays:

// This is an associative array
$x = array("one" => 1,
           "two" => 2,
           "three" => 3);
  
// This prints 1
echo $x["one"];
  
// This is a numerically indexed array
$x = array(1, 2, 3);
  
// This prints 2
echo $x[1];

Two functions are useful for checking the state of a variable:

// Has the variable been declared?
if (isset($x))
  echo "x is set";
  
// Is the variable empty?
if (empty($x))
  echo "x is empty";

A variable that doesn’t exist is always empty. However, a variable that’s empty may or may not exist. If it does, it has a NULL value.

Variables are assigned with a single = character, and equality is tested with the double equals (==) syntax:

$x = 4;
  
if ($x == 4)
  echo "x is four!";

A triple equals (===) can be used to test if the parameters are equal and of the same type:

$x = 0;
  
// This is true
if ($x == false)
  echo "$x is false";
  
// This is false
if ($x === false)
  echo "$x is false";

The arithmetic shortcuts that work in many other languages also work in PHP:

$x = 4;
  
// add one to $x in three different ways
$x++;
$x = $x + 1;
$x += 1;
  
// subtract one in three different ways
$x--;
$x = $x - 1;
$x -= 1;

The standard loop constructs are for , while , and do...while :

for ($x=0; $x<10; $x++)
  echo $x;
  
$x = 0;
while ($x < 10)
{
  echo $x;
  $x++;
}
  
$x = 0;
do
{
  echo $x;
  $x++;
} while ($x < 10);

The foreach statement is used to iterate through an array:

// $x is an array of strings
$x = array("one", "two", "three", "four", "five");
  
// This prints out each element of the array
foreach ($x as $element)
  echo $element;

The if and switch statements are the most frequently used conditionals:

if ($x < 5)
  echo "x is less than 5";
  
switch ($x)
{
  case 1:
     echo "x is 1";
     break;
  case 2:
     echo "x is 2";
     break;
  case 3:
     echo "x is 3";
     break;
  default:
     echo "x is not 1, 2, or 3";
}

There is other basic syntax that isn’t discussed here. There are also over a hundred libraries that can be used for tasks as diverse as string manipulation, network communications, data compression, and disk access. If you’d like more detail, a list of references is included at the end of this chapter in Section 11.8.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset