25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

true, the code inside the loop will never be executed. In a do...while() loop, on the<br />

other hand, the check takes place at the end of each iteration of the loop—meaning<br />

that, even if the condition never evaluates to true, the contents of the loop will be<br />

executed at least once.<br />

The for and foreach constructs are specialized looping mechanisms that can be<br />

used to essentially encapsulate a while() loop in a slightly more readable form:<br />

for ($i = 0; $i < 10;$i++) {<br />

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

}<br />

i<br />

As you can see, the for declaration contains three portions, separated by semicolons.<br />

The first one contains an instruction (or series of instructions separated by a comma)<br />

that is executed once before the loop has begun. The second one contains a condition<br />

that is checked at the beginning of every iteration the loop, and the third one an<br />

instruction (or, again, a set of instructions separated by a comma) that is executed at<br />

the end of every iteration. Therefore, the code above would be equivalent to writing<br />

the following:<br />

$i = 0;<br />

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

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

$i++;<br />

}<br />

The built-in <strong>PHP</strong> _EOL constant represents the “end of line” marker for your current operating<br />

system.<br />

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

Similar to for is the foreach construct, which allows you to loop through an array;<br />

we discuss this construct in the Arrays chapter.

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

Saved successfully!

Ooh no, something went wrong!