25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>PHP</strong> Basics ” 29<br />

// Evaluates to true<br />

break;<br />

default:<br />

// Will only be executed if no other conditions are met<br />

break;<br />

}<br />

A switch statement evaluates the initial expression ($a in this case) only once, and<br />

then compares it against the individual case values; if a match is found, it will continue<br />

to execute code until it encounters a break statement. Note that the use of<br />

break is required—or the interpreter will continue executing code even if it finds another<br />

case. Finally, if none of the test cases match, the interpreter executes the code<br />

block in the default block.<br />

Iterative Constructs<br />

Iterative constructs make it possible to execute the same portion of code multiple<br />

times. <strong>PHP</strong> has four of these, although only two of them are necessary to the functioning<br />

of a language.<br />

The simplest iterative constructs are the while() and do...while() loops; they allow<br />

you to perform a series of operations until a condition evaluates to false:<br />

$i = 0;<br />

while ($i < 10) {<br />

echo $i . <strong>PHP</strong>_EOL;<br />

$i++;<br />

}<br />

$i = 0;<br />

do {<br />

echo $i . <strong>PHP</strong>_EOL;<br />

$i++;<br />

} while ($i < 10);<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

As you can see, these two types of loop are very similar; the only significant difference<br />

is in when the condition is checked to determine whether the code inside the<br />

construct should be executed or not. In a while() loop, the check is performed every<br />

time the execution point enters the loop—this means that, if the condition is never

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!