Control Structures - PHP Tutorial

Control Structures

Code

if

This is used for decision making. The code between the curly brackets is executed if the Boolean is true.

A Boolean is a statement that is either true or false. For example

($input == 0)

If $input is zero, the Boolean is true.

If $input has any other value the Boolean is false.

Remember that "==" means "is equal to" and don't confuse it with "$input = 0". This would store a zero into $input and be treated as "False". (This is a common coding error).

// =======================================================
// TEMPLATE
// =======================================================
if (Boolean)
{
  // Code block
}

// =======================================================
// EXAMPLE
// =======================================================
if ($hour < 12)
{
  echo "Good morning!";
}

if with else

This is used for either/or decisions. If the Boolean is true, the first block of code is executed. If the Boolean is false, the second code block is executed.

// =======================================================
// TEMPLATE
// =======================================================
if (Boolean)
{
  // Code block
}
else
{
  // Code block
}

// =======================================================
// EXAMPLE
// =======================================================
if ($hour < 12)
{
  echo "Good morning!";
}
else
{
  echo "Good afternoon!"
}

if with else if

This is used to test for a series of conditions.

There can be as many "else if" blocks as necessary.

There is often a final "else" without an "if".

With this structure, only one block of code will be executed depending on which Boolean was true. If there is a final "else" without an "if", this block will execute if none of the other Booleans were true.

// =======================================================
// TEMPLATE
// =======================================================
if (Boolean)
{
  // Code block
}
else
if (Another_Boolean)
{
  // Code block
}
else  // Optional final else
{
  // Code block
}

// =======================================================
// EXAMPLE
// =======================================================
if ($hour < 12)
{
  echo "Good morning!";
}
else
if ($hour < 16)
{
  echo "Good afternoon!"
}

else
{
  echo "Good night!"
}

for

Use a for loop if you know, or can calculate in advance, how maby repetitions are needed.

The for loop is used to repeat the code block between the curly brackets. "for" loops are often used for counting too.

"start" determines how the repetition starts. Here is an example.

$counter = 0; // The loop begins
              // with $counter set
              // to zero.

"stop" is a Boolean that determines whether the repetition should finish. If the Boolean is true, the repetition continues. When the Boolean goes false, the repetition ends. A common bug is to write a Boolean that never goes false. This loop repeats for ever!

$counter <= 10; // The loop repeats
                // as long as $counter
                // is less than or equal
                // to 10.

"step" .

$counter = $counter + 2; // Counter is
                         // increased by
                         // two on each
                         // repetition.

// =======================================================
// TEMPLATE
// =======================================================
for(start; stop; step)
{
  // Code block
}

// =======================================================
// EXAMPLE
// =======================================================
for($counter = 0; $counter <= 10; $counter = $counter + 2)
{
  // Code block
}
// =======================================================

 

while

The while loop Boolean comes before the repeated block of code. This means that the repeated code could be executed zero or more times.

As long as the Boolean is true, the loop will repeated.

// =======================================================
// TEMPLATE
// =======================================================
while(Boolean)
{
  // Code block
}

// =======================================================
// EXAMPLE
// =======================================================
while($counter <= 10)
{
  echo "$counter\n";
}
// =======================================================

do

The do loop Boolean comes after the repeated block of code. This means that the repeated code block will be executed at least once.

The loop repeats as long as the Boolean is true.

// =======================================================
// TEMPLATE
// =======================================================
do
{
  // Code block
}
while(Boolean);

// =======================================================
// EXAMPLE
// =======================================================
do
{
  echo "Make your choice ("q" or "Q" to quit) ... ";
  $choice = trim(fgets(STDIN));
}
while(!(($choice == "q") or ($choice == "Q")));
// =======================================================

function

Used to modularise programs.

 

// =======================================================
// TEMPLATE
// =======================================================
function ($foo, $baa)
{
  // Code Block
}

// =======================================================
// EXAMPLE
// =======================================================
function multiply($n1, $n2)
{
  return $n1 * $n2;
}
// =======================================================
function double(&$n)
{
  $n = 2 * $n; // The & specifies a call by reference.
}

// =======================================================

echo "Please enter n1 ... ";
$n1 = fgets(STDIN);

echo "Please enter n2 ... ";
$n2 = fgets(STDIN);

$ans = multiply($n1, $n2);

echo "\n\n$ans\n\n";

// =======================================================

 

Comparison Operators

Example Name Result
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP >=4 only)
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP >=4 only)
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.

 

Logical Operators

Example Name Result
$a and $b And TRUE if both $a and $b are TRUE.
$a or $b Or TRUE if either $a or $b is TRUE.
$a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.
$a || $b Or TRUE if either $a or $b is TRUE.

 

Arithmetic Operators

Example Name Result
$a + $b Addition Sum of $a and $b.
$a - $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.

 

Learning Tasks

To become fluent in PHP programming, you need to know the details on this page very well.

It's not easy but get learning!