loops php 04
DESCRIPTION
PHP course Lecture 4 Contents PHP Array Functions while - loop do...while for - loop foreachTRANSCRIPT
Instructor Name: Mohamed Saad.
Email: [email protected]
Occupation: Web Developer In IT Big Dig.
PHP Course [email protected]
Contents• PHP Array Functions
owhile - loopodo...while o for - loop o foreach
PHP Course [email protected]
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.
<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]
For LoopSyntax
o for (Counter; condition; increment) { code to be executed; }
PHP Course [email protected]
<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]
Insert number and you will get
textboxes equals that number
<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]
do...while StatementSyntax
o do { code to be executed; }while (condition);
PHP Course [email protected]
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.
<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]
This will not work<?php foreach (array(1, 2, 3, 4) as &$value)
{ $value = $value * 2;
}?>
We have to create variable first.
We hope You enjoy This Tutorial.
For any Suggestions Please Email Us
PHP Course [email protected]
EndLoops