PHP Programming

PHP supports many of the same programming constructs that are common in other programming languages, including Javascript. What follows is a brief introduction to some of its features.

Variables in PHP start with a $ followed by the name, which must begin with a letter or underscore and can contain only alphanumeric characters. There is no command for declaring variables in PHP. A variable is created as soon as you assign it a value.

<?php
$pi = 3.14;
$name = “John Doe”;
$_flag = true;
?>

PHP variables include integers, floating point numbers, strings, Booleans, and arrays. A variable can be assigned the value null to give it no value. Variables declared within functions are local and can only be accessed within the function. Variables defined outside of functions are global in scope. The global keyword is used to denote global variables inside functions.

Conditionals are supported through if…else statements, such as shown here:

<?php

$t=date(”H”);   //This built-in function retrieves the hour of day

if ($t<”20”) {

echo “Have a good day!”;

} else {

echo “Have a good night!”;

}

?>

PHP comparison operators for conditional statements, like those in Javascript, include:

  • ==             equal to
  • !=              not equal to
  • ===           identical to (equal to and of the same data type)
  • !==           not identical to
  • >                greater than
  • >=             greater than or equal to
  • <                less than
  • <=             less than or equal to

Loops can be used in PHP. Several types of loops are supported including: for and while loops. Here are a couple of examples.

<?php

for ($i=0; $i<10; $i++)   {

echo(“The number is now:“ $i <br>”;    //This loop counts from 0 to 9

}

?>

<?php

$x = 1;

while ($x <= 5) {

echo “The number is now: $x <br>”;   //This loop counts from 1 to 5

$x++;

}

?>

Functions are blocks of code that are only executed when called (referenced) by other code. You have already seen examples of Javascript functions. PHP functions are similar.

Here is an example of a function that would accept two values as arguments and return the resulting sum.

<?php

function sumNumbers($a,$b)   {

return $a + $b;

}

?>

This function might be called with a block of code such as:

<?php

echo “5 + 10 = “ . sumNumbers(5,10) . “<br>”; //The period is used for string concatenation

echo “17 + 13 = “ . sumNumbers(17,13);

?>

After execution, this code would yield:

5 + 10 = 15

17 + 13 = 30

PHP has a number of built-in functions. A small sampling of these is given here:

  • echo(string) — outputs one or more strings
  • print(string) — outputs one string only; always returns 1
  • strlen(string) — returns the length of a string
  • strpos(string, substring) — returns the position of a character of substring within a string
  • array() — creates an array
  • count() — counts the number of elements in an array
  • sort() — sorts an indexed array in ascending order
  • min() — returns the lowest value in an array or the lowest of a set of values
  • max() — returns the highest value in an array or the highest of a set of values
  • date(format, timestamp) — return date and/or time
  • fopen(file) — opens a file
  • fwrite(file) — writes data to a file
  • sleep(secs) — delays code execution for a number of seconds

PHP is also used in combination with MySQL to connect to and manipulate databases. For information about these and other functions in PHP, consult additional resources.