Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
                        break;
case 2:
echo "Cat age is 2";
break;
case 3:
echo "Cat age is 3";
break;
case 4:
echo "Cat age is 4";
break;
default:
echo "Cat age is unknown";
}
?>

Although it is only slightly shorter, Listing 47.4 is a great deal more readable
and much easier to maintain. A switch/case group is made up of a
switch() statement in which you provide the variable you want to check,
followed by numerous case statements. Notice the break statement at the
end of each case. Without that, PHP would execute each case statement
beneath the one it matches. Calling break causes PHP to exit the
switch/case. Notice also that there is a default case at the end that
catches everything that has no matching case.


It is important that you not use case default: but merely default:.
Also, it is the last case label, so it has no need for a break statement
because PHP exits the switch/case block there anyway.


Loops


PHP has four ways you can execute a block of code multiple times: by using
while, for, foreach, or do...while. Of the four, only do...while sees
little use; the others are popular, and you will certainly encounter them in
other people’s scripts.


The most basic loop is the while loop, which executes a block of code for as
long as a given condition is true. So, you can write an infinite loop—a
block of code that continues forever—with this PHP:


Click here to view code image
<?php
$i = 10;
while ($i >= 10) {
$i += 1;
echo $i;

Free download pdf