Download - Loops PHP 04
![Page 2: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/2.jpg)
Instructor Name: Mohamed Saad.
Email: [email protected]
Occupation: Web Developer In IT Big Dig.
PHP Course [email protected]
![Page 3: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/3.jpg)
Contents• PHP Array Functions
owhile - loopodo...while o for - loop o foreach
PHP Course [email protected]
![Page 4: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/4.jpg)
PHP Loopswhile - loop•Loops through a block of code while a specified condition is true.
do...while •Loops through a block of code once, and then repeats the loop as long as a specified condition is true.
for - loop•Loops through a block of code a specified number of times.
foreach •Loops through a block of code for each element in an array.
![Page 6: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/6.jpg)
![Page 7: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/7.jpg)
<html> <body>
<form action="while_loop.php" method="post"> Enter any number less than 10000:<input type="text" name="x"/> <input type="submit" name="submit" value="Go"/> </form> </body></html>
<?phpif (empty($_POST)===false){
$x=$_POST['x'];if ($x<10000){
$i=1;while($i<=$x){
echo $i . "<br>";$i++;
} }//end of nasted if
else{echo "You entered invalid value";}}//end of empty($_POST)
?>
Copy Codewhile_loop.php
PHP Course [email protected]
![Page 8: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/8.jpg)
For LoopSyntax
o for (Counter; condition; increment) { code to be executed; }
PHP Course [email protected]
![Page 9: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/9.jpg)
![Page 10: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/10.jpg)
<html> <body>
<form action="for_loop.php" method="post"> Enter any number less than 10000:<input type="text" name="x"/> <input type="submit" name="submit" value="Go"/> </form> </body></html>
<?phpif (empty($_POST)===false){
$x=$_POST['x'];if ($x<10000){
for ($i=1; $i<=$x; $i++){
echo $i . "<br>";}
}//end of nested ifelse{echo "You entered invalid value";}
}//end of empty($_POST) ?>
Copy CodeFor_loop.php
PHP Course [email protected]
![Page 11: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/11.jpg)
Insert number and you will get
textboxes equals that number
![Page 12: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/12.jpg)
![Page 13: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/13.jpg)
<html> <body>
<form action="for_loop.php" method="post"> Enter any number less than 10000:<input type="text" name="x"/> <input type="submit" name="submit" value="Go"/> </form> <?php
if (empty($_POST)===false){
$x=$_POST['x'];if ($x<10000){
for ($i=1; $i<=$x; $i++){?>
<form action="for_loop.php" method="post"> <input type="text" name="<?php $i; ?>" value="<?php echo $i; ?>"/>
<?php }
}//end of nested ifelse{echo "You entered invalid value";}
}//end of empty($_POST) ?> <input type="submit" name="submit" value="Go"/></form></body></html>
Copy CodeFor_loop.php
PHP Course [email protected]
![Page 14: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/14.jpg)
do...while StatementSyntax
o do { code to be executed; }while (condition);
PHP Course [email protected]
![Page 15: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/15.jpg)
![Page 16: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/16.jpg)
For Each• When foreach first starts executing, the internal
array pointer is automatically reset to the first element of the array.
• In order to be able to directly modify array elements within the loop precede &$value.
![Page 17: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/17.jpg)
![Page 18: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/18.jpg)
<pre><?php$x = array(1, 2, 3, 4);print_r($x); echo "<br>";foreach ($x as &$value) { $value = $value * 2;echo $value;echo "<br>----------<br>";}//unset or it will equal 8unset($value);print_r($x); ?></pre>
Copy Codeforeeach.php
PHP Course [email protected]
![Page 19: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/19.jpg)
This will not work<?php foreach (array(1, 2, 3, 4) as &$value)
{ $value = $value * 2;
}?>
We have to create variable first.
![Page 20: Loops PHP 04](https://reader034.vdocument.in/reader034/viewer/2022052200/556214b4d8b42a00138b5188/html5/thumbnails/20.jpg)
We hope You enjoy This Tutorial.
For any Suggestions Please Email Us
PHP Course [email protected]
EndLoops