2. Instructor
Name: Mohamed Saad.
Email: Engsaad_aly@hotmail.com
Occupation: Web Developer In IT Big Dig.
PHP Course Info@ITBigDig.com
3. Contents
• PHP Array Functions
o while - loop
o do...while
o for - loop
o foreach
PHP Course Info@ITBigDig.com
4. while - 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.
5. while - loops
Syntax
o while (condition)
{
code to be executed;
}
PHP Course Info@ITBigDig.com
6.
7. <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>
<?php
if (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 Code
while_loop.php
PHP Course Info@ITBigDig.com
8. For Loop
Syntax
o for (Counter; condition; increment)
{
code to be executed;
}
PHP Course Info@ITBigDig.com
9.
10. <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>
<?php
if (empty($_POST)===false)
{
$x=$_POST['x'];
if ($x<10000){
for ($i=1; $i<=$x; $i++)
{
echo $i . "<br>";
}
}//end of nested if
else{echo "You entered invalid value";}
}//end of empty($_POST)
?>
Copy Code
For_loop.php
PHP Course Info@ITBigDig.com
16. 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.