PHP Basics: Control Structures
In this section of the Blog, their will be examples of Control Structures in PHP. This includes If and Switch statements, including the four types of loops. Not included in this section are objects and functions as they are documented in later posts. ZCE Zend Certification will cover Control Structures in detail in many sections.
Types of Control Structures:
– If
– Switch
– While
– do
– For
– – Continue
– – break
– Foreach
Control Structure: IF
Probably the most common control structure, IF’s are easy to use once you have implemented a few into scripts, therefore I wont go on about them. As you should already be very aware:
if ($a === 5){ echo "a is identical to 5"; }elseif ($a == 5){ echo "a is equal to 5"; }else{ echo "a is neither equal to or identical to 5"; }
Their are some other syntax that is rarely used, possibly depreciated:
if ($a == 5): echo "they are equal"; elseif ($a === 5): echo "they are identical"; endif;
Control Structure: Short IF
(The Ternary Operator)
You can use SHORT If statements to save on line space, they are not often used. The bellow code is similar to if action post is empty then action is default, otherwise its what it was set.
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
Control Structure: Switch
Switch is faster than IF Statements as they rely on one variable and have all possible data in the switch. They are simple enough, You should also be aware of switch statements, so theirs not much to be said.
switch($a){ case 1: echo "1"; break; case 'two': echo "2"; break; default: echo "3"; }
Control Structure: Do While (LOOP)
A Do While Loop executes its content, then depending on the following conditional statements it may loop trough again. The bellow example will output the data once:
$cookies = 0; do { echo "Mmmmm...I love cookies! *munch munch munch*"; } while ($cookies > 1);
The data is displayed once as, $cookies is 0 and therefore not greater than 1. The following example though will output the data 4 times.
$cookies = 0; do { echo "$cookies Mmmmm...I love cookies! *munch munch munch*"; $cookies++; } while ($cookies < 5);
Control Structure: While (LOOP)
While acts very similar to Do While except that it does not execute its contents before checking the conditional statement. Therefore it ensures it fits the condition then loops trough its contents until it no longer fits the condition.
$a = 0; while ($a++ < 10){ echo $a; }
The above code for example will print the number up to the value of 9, once this value has been reached it will stop looping and continue with the script.
Control Structure: For (Loop)
Possibly the most common Loop used in PHP the For loop, the example bellow sets the loop variable to 0, then implements the condition ensuring that $a has to be lower than 10 finally each time the loop is initiated it increases $a by one. The output therefore will range to 9.
for ($a = 0; $a < 10; $a++){ echo $a; }
Control Structure: For Each (Loop)
This type of loop is commonly used in connection with arrays. The bellow example works with an array contained in the $students variable, the KEY in the array are the students name ($name) and the value is the students grade ($grade). You can imagine that the key ($name) points => to the value ($grade).
foreach ($students as $name => $grade){ echo "$name got a $grade"; }
As you can see, for each student in the array this code will output their name, followed by the grade they received.
Control Structure: Break
The break statement is used to stop the execution of a loop then continue with the rest of the script. If their is a Loop within a Loop the break statement will end the embedded loop and continue .
As an example the while loop below will stop when $number equals to 6.
$number = 1; while ($number < 10){ echo $number . '<br>'; if ($number == 6){ break; } $number += 1; }
1 Comment »
RSS feed for comments on this post. TrackBack URL












Great work.