Loops are used when you want to execute a block of code a specified number of times, or while a specified condition is True.
while (condition) { // code to be executed }If the condition starts out as false, the statements won’t execute at all.
<?php $i = 0; while ($i<5) { echo '<br /> i = '. $i; $i++; } ?>- First it's declared a variable "$i" with the value of 0. The "while" statement checks the condition ($i<5), which it's True and permits the execution of block code inside the brackets. The "$i++" increments the value of "$i" by 1 each time the loop runs, and check again the condition. The loop will stop when the "$i" reach 5.
do { // code to be executed } while (condition)First will be executed the block of code, then checks the condition, and it will repeat the loop as long as the specified condition is TRUE.
<?php $x = 8; do { echo '<br /> x = '. $x; $x++; } while ($x<5) ?>- This example display "x = 8".
while (condition) { if(end_condition) break; // code to be executed }If the end_condition is True, the code after the "break" will not be executed at all, and the execution of "while" loop ends.
<?php $i = 0; while ($i<5) { if ($i==2) break; echo '<br /> i = '. $i; $i++; } ?>- Output:
<form action="script.php" method="post"> ... </form>
#id { width: 100px; word-wrap: break-word; }
var tutorials = ["php", "html", "css", "flash"]; tutorials.sort(); alert(tutorials[0]); // css
$code =[10=>"Perl", 20=>"PHP", 21=>"Python", 30=>"JavaScript"); $last = end($code); echo $last; // JavaScript