PHP Reference: Beginner to Intermediate PHP chapter 2 Class 2.
Control Structures
The heart of PHP is the control structures. Variables and arrays are lonely without them as they facilitate comparisons, loops, and large hands telling you to go that way and do it this way. Okay, I made that last part up. Here we go!
If, ElseIf, Else
if (expr) {
// If expr is TRUE, do this, then exit the IF loop }elseif (expr2) {
// If expr is FALSE, and expr2 is TRUE, do this, then exit the loop }else{
// If all expr's are FALSE, do this, then exit
}
There can be only one instance of else in an if statement, but multiple elseif expressions are allowed prior to the else statement.
Example:
$x = 1;
if ($x < 1){
echo '$x is less than 1';
}elseif ($x == 1){ // Note the double equals, for comparison echo '$x is equal to 1';
}else{
echo '$x is neither equal to 1 or less than 1';
}
$x is equal to 1
See Also:
switch – A simpler, more organized usage than multiple if/elseIf combinations
break – Stops a loop and exits regardless of if the statement evaluates as true
Alternative syntax for an if statement:
if (expr):
// If expr is TRUE, do this, then exit the IF loop elseif (expr2):
// If expr is FALSE, and expr2 is TRUE, do this, then exit the
loop else:
// If all expr's are FALSE, do this, then exit
endif;
Switch
switch (expr) {
case value:
// Do this if value matches break;
case value2:
// Do this if value2 matches break;
default: // [optional]
// Do this if no other cases match. Does not have to be at the end break;
}
expr – A $string, $integer, or $float to be compared against
A switch evaluates the expr against any number of cases or options, specifying the behavior for each case.
Cases can be 'stacked' to allow the same portion of code to be evaluated for different cases:
switch (expr) {
case value:
case value2:
// Do this if value or value2 matches
}
The switch is evaluated line-by-line, and therefore if there was no break command, the case declaration would effectively be ignored and the code would continue to be processed until the switch ends or a break; is reached.
$x = 1;
switch ($x) {
case 1:
echo '1'; // Note the lack of a break; case 2:
echo '2'; // Without the break, this is processed line-by-line
}
Finally, the default statement is optional, but defines what to do if no cases are matched. It can be used in troubleshooting to identify when you failed to include a case for an expected output.
Examples:
$x = 2;
switch ($x) {
case 1:
echo '1';
break;
case 2:
echo '2';
break;
case 3:
echo '3';
break;
}
$x = 'howdy';
switch ($x) {
case 'hi':
echo 'Hi there';
break;
default: // Can be anywhere, all cases evaluated before it is used echo 'Greetings';
break;
case 'hello':
echo 'Hello there';
break;
}
Greetings
See Also:
break – Stops a loop and exits regardless of if the statement evaluates as true
Alternative syntax for a switch statement:
switch (expr):
case value:
// Do this if value matches break;
case value2:
// Do this if value2 matches break;
default: // [optional]
// Do this if no other cases match. Does not have to be at the end break;
endswitch;
while
while (expr) {
// If expr is TRUE, do this, then evaluate expr again
}
The while loop checks the expr and if it evaluates as true, the script runs through the entire contents of the while until it finishes, then it evaluates the expr again and repeats until the expr evaluates as false.
Example:
$x = 1;
while ($x <= 3){
echo "$x, ";
$x++; // increments $x by adding 1. Short-hand version
}
1, 2, 3,
See Also:
do-while – Same as while, except the expr is evaluated after the first action break – Stops a loop and exits regardless of a TRUE statement evaluation continue – Stops the iteration of the loop, and the expr is evaluated again
Alternative syntax for a while statement:
while (expr):
// If expr is TRUE, do this, then evaluate expr again endwhile;
do-while
do {
// Do this
} while (expr);
The do-while loop performs whatever is inside the do statement, checks the expr, then if it evaluates as TRUE, runs through the entire contents of the do until it finishes, evaluating the expr again, and repeating until the expr evaluates as FALSE.
Example:
$x = 1;
do {
echo "$x, ";
$x++; // Makes $x = 2, therefore the while will evaluate as false
} while ($x <= 1); 1,
See Also:
while – Similar to do-while, except the expr is evaluated first
break – Stops a loop and exits regardless of if the statement evaluates as true continue – Stops the iteration of the loop, and the expr is evaluated again
No comments:
Post a Comment