PHP introduction

There are different versions of PHP with different features. This tutorial will be mainly related to versions 4 and 5.

Installation

To manually install PHP use these web sites:

Or use this tool:

Variables and arrays

Variables in PHP are denoted by the $ sign followed by the name of the variable. You do not have to specify a type for a variable, and a variable can change type throughout a program.

Strings

$string = 'my first string';
echo $string2 = "Quote - $string"; //output: Quote - my first string

In PHP strings can either be denoted by single or double quotes. Single quotes are the basic strings that hold exactly what is typed whereas double quotes process the data inside them. Within double quotes variable names get changed to there value, and special characters are allowed (e.g. newline \n, \r). You can also specify a string using this other syntax.

$string = 'string';
echo $string2 = <<<EOT
my string \$ -
\t "$string"
EOT;

//outputs 
my string $ -
     "string"
NB: The syntax of the above string must start with a <<<EOT and then have a new line and end with a EOT; which also must be on a new line.

This syntax is the same as the syntax for the double quotes, variables can be quoted and special characters are allowed (note that the $ symbol must be proceeded by a slash).

Booleans

In PHP booleans are represented by the constants TRUE and FALSE. The following code illustrates what is equivalent to true and false.

//All of these output true
var_dump(1 == TRUE);
var_dump(0 == FALSE);
var_dump(-1 == TRUE);
var_dump(2.718 == TRUE);
var_dump('0' == FALSE);
var_dump('' == FALSE);
var_dump(NULL == FALSE);
var_dump(0.0 == FALSE);
var_dump(array() == FALSE);

Note that zero is equivalent to false and every other number is equivalent to true. Empty strings and empty arrays also evaluate to false. The var_dump function simply prints a string representation of its argument.

Integers and floating point

Integers and floating point numbers can be represented as follows.

$int = 5;
$floating_point = 1.414;
$var = 1.6E-19;

Constants

In PHP constants are defined using the function define and then can used throughout a script by their symbolic name.

define('CONSTANT', 10);
echo CONSTANT; //outputs: 10

Arrays

Arrays in PHP are declared using the array function.

$array = array('a', 'b', 'c');
$array2 = array('key' => 'value', 1 => 'value2');

When an array is declared you can specify either a string or an integer for the key and specify key value pairs. To get a value out of an array use the bracketed syntax.

echo $array[0];
echo $array2['key'];

Arrays do not have a fixed size and can be changed easily using the various functions.

push($array, 'string');
echo pop($array); //outputs: string
$array[] = 'string2';

The two functions push and pop can be used to add and remove values from an array, also the empty bracket syntax can be used to add an entry to the bottom of the array.

Conditional statements

Below is an example of an if-then-else statement.

if($bool)
   echo 'true';
else
   echo 'false';

The above code only has one statement for each branch and so no brackets are necessary however if more than one statement is needed then curly-brackets are needed.

if($bool2)
{
   echo 'variable is ';
   echo 'true';
}
else
{
   echo 'variable is ';
   echo 'false';
}

Switch statements

Switch statements can be used to replace multiple it-then-else statements.

switch($variable)
{
   case 'Value1':
      echo 'The variable has value: Value1';
      break;
   case 'Value2':
   case 'Value3':
      echo 'The variable has value: Value2 or Value 3';
      break;
   default:
      echo 'The variable's value is not Value1, Value2 or Value 3.';
      break;
}

Note that break statements are needed between cases otherwise multiple cases are considered at once.

Loops

In PHP there are many types of looping structures including for, while, do-while and foreach.

while($bool = myBoolFunction())
{
   echo $bool;
}

The while loop takes this basic form. The code is run until the condition evaluates to false.

do
{
   echo 'true';
}
while(myBoolFunction())

The do-while loop is very similar to the while loop however this loop will run at least once.

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

For loops have three parts to there structure, the first initialises a variable, the second is a condition that is tested after every loop and finally an incremental stage to change a variable.

foreach($array as $element)
{
   echo $element;
}

The foreach loop is designed to make array looping easy. The following code gets every element of the array $array and prints it out. This loop can also be used in conjunction with array keys as well as values.

foreach(array(0 => 'a', '1' => 'b') as $key => $value)
{
   echo '('.$key.' => '.$value.') ';
}
//outputs: (0 => a) (1 => b)

Functions

Functions can be declared using the following syntax.

function myFunction($arg1, $arg2 = 'optional')
{
   return $arg1.$arg2;
}

This function simply returns the value of the two arguments concatenated. Note that the second argument is given a default value so the function can be called without specifying the second argument.

function manyArgs($arg1)
{
   print_r(func_get_args());
   echo func_num_args();
   echo func_get_arg(0);
}

This function illustrates the use of the built in functions that handle function arguments. The first line prints a list of all the arguments, the second prints the number of arguments and the third returns the value of the first argument. This function has to be called with at least one argument but can also be called with many.

Including files

If you have split your PHP code into multiple pages you can include the pages you need using either require or include.

include("functions.php");
require("functions.php");

The difference between these functions is that if the file is not found then require will stop the page from being run whereas include will continue even though the page has not been included.

include_once("functions.php");
require_once("functions.php");

These functions behave like those above but only include the data from the pages once.

Categories

Tags

Social