PHP Control Structures
Decisions and Loops
Decisions
Selection is necessary in programming languages - PHP has a rich collection of decision structures, and a fairly easy syntax to use, so long as you remember you are using a C variant, as such comparison operators and assignment operators often get mistaken, so be careful!
Simple Binary Selection
Like most languages, simple binary selection takes place using the if statement. Unlike Delphi, PHP does not have a THEN keyword, and wraps the structure up a little differently:
<?php
$x = rand(1,10);
if ($x <=5) {
echo $x.' is less than 5';
} else {
echo $x . ' is greater than 5';
}
?>
Notice: the condition is in brackets, the "then" en a set of braces and the else (optional) tacks on an end set of braces as "begin end" containers for code to be conditionally executed.
If statements can be nested inside others, making for some lovely rich logic, and the potential for trickey logic errors so be careful!
Multiple Selection
There are two common ways to effect multiple decisions, or a sequence of related decisions and they vary in efficiency.
There is an elseif replacement for the else that allows you to chain together related decisions:
<?php
$dice = rand(1,6);
if ($dice==1){
echo '[.]';
} elseif ($dice==2){
echo '[:]';
} elseif ($dice==3){
echo '[:.]';
} elseif ($dice==4){
echo '[::]';
} elseif ($dice==5){
echo '[::.]';
} else {
echo '[:::]';
}
?>
In the above fragment, a dice roll is rendered as [.] .. [:::] but best case it asks one question, worst case it asks 5 questions to generate output and as such is not as efficient as the switch statement equivalent:
<?php
$dice = rand(1,6);
switch($dice){
case 1:
echo '[.]';
break;
case 2:
echo '[:]';
break;
case 3:
echo '[:.]';
break;
case 4:
echo '[::]';
break;
case 5:
echo '[::.]';
break;
default:
echo '[:::]';
break;
}
?>
Note in the above structure it asks ONE question best and worst case, jumping straight to the relevent value handler. NOTE that BREAKS are needed to tell the script NOT to continue execution down through the instruction list.
The break command merely jumps OUTSIDE the switch statement to the instruction immediately below it, if there is any, and resumes execution there. Note the optional use of the "default" case which can be used as a catch-all for any un-listed case values (or in this case it is being used as an "else" during the process of elimination when displaying dice outcomes)
Loops
Iteration is important, computers are great at repeating things and as such PHP has a rich collection of loop commands.
As you possibly know, loops fall into 2 categories - good programming is using the BEST loop for the job.
(a) Definite iteration
<?php for ($i = 1; $i<=5; $i++){ task; } ?>
The for loop is similar to the for to do loop in Lazarus. It needs a numeric loop variable as a loop counter - interestingly this can be introduced when first encountered in the for loop command. You need to give it a starting value, a condition that when it fails stops the loop, and an increment/decrement operator.
In the above example, the loop variable $i will take values 1..5, looping the task 5 times. Naturally, if the condition can NEVER become false, the loop will be infinite (which may be a bad thing, particularly for the server).
It is usual to use a POST increment in a for loop as a pre-increment can result in one less loop cycle - why?
There is a variation that works for arrays, the foreach:
<?php
$dwarves = array('lumpy','twitchy','sweary','bleary');
foreach ($dwarves as $thingy){
echo $thingy;
}
?>
The foreach loop above moves through each array element in turn and echos them out. it is functionally equivalent to:
<?php
$dwarves= array('cheesy','snarly','smelly','whiney');
for ($i = 0; i<=3; $i++){
echo $dwarves[$i];
}
?>
but possibly tidier.
(b) Indefinite iteration
Commonly, "while do" and "do while" loops are the indefinite arsenal of PHP
they are fairly logical
while (condition is true) do { stuff
}
and
do {
stuff
} while (condition is true);
eg:
$x = 0;
while ($x < 10) do {
echo $x.'<br />'; $x++;
}
Note, this is the PRE-TESTED form of this loop - the looped task may not get executed at all depending on the condition.
The do--while is POST-TESTED and the loop contents will be executed at least once regardless - good to know.
|