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:
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250"> <param name="src" value="file.swf" /> Your browser not support SWF. </object>
input:focus { background-color: #88fe88; }
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}'; var obj = JSON.parse(jsnstr); alert(obj.url);
if (file_put_contents("file.txt", "content")) echo "The file was created"; else echo "The file can not be created";