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:
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net