PHP Course
PHP Course Info@ITBigDig.com
Loops
Instructor
 Name: Mohamed Saad.
 Email: Engsaad_aly@hotmail.com
 Occupation: Web Developer In IT Big Dig.
PHP Course In...
Contents
• PHP Array Functions
o while - loop
o do...while
o for - loop
o foreach
PHP Course Info@ITBigDig.com
while - loop
•Loops through a block of code while a specified condition is true.
do...while
•Loops through a block of code...
while - loops
Syntax
o while (condition)
{
code to be executed;
}
PHP Course Info@ITBigDig.com
<html>
<body>
<form action="while_loop.php" method="post">
Enter any number less than 10000:<input type="text" name="x"/>
...
For Loop
Syntax
o for (Counter; condition; increment)
{
code to be executed;
}
PHP Course Info@ITBigDig.com
<html>
<body>
<form action="for_loop.php" method="post">
Enter any number less than 10000:<input type="text" name="x"/>
<i...
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"/>
<i...
do...while Statement
Syntax
o do
{
code to be executed;
}
while (condition);
PHP Course Info@ITBigDig.com
For Each
• When foreach first starts executing, the internal
array pointer is automatically reset to the first
element of ...
<pre>
<?php
$x = array(1, 2, 3, 4);
print_r($x);
echo "<br>";
foreach ($x as &$value) {
$value = $value * 2;
echo $value;
...
This will not work
<?php
foreach (array(1, 2, 3, 4) as &$value)
{
$value = $value * 2;
}
?>
We have to create
variable fir...
We hope You enjoy This Tutorial.
For any Suggestions Please Email Us
Info@ITBigDig.com
PHP Course Info@ITBigDig.com
End
Lo...
Loops PHP 04
Loops PHP 04
Loops PHP 04
Loops PHP 04
Loops PHP 04
Upcoming SlideShare
Loading in …5
×

Loops PHP 04

1,562 views

Published on

PHP course Lecture 4
Contents
PHP Array Functions
while - loop
do...while
for - loop
foreach

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,562
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
45
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Loops PHP 04

  1. 1. PHP Course PHP Course Info@ITBigDig.com Loops
  2. 2. Instructor  Name: Mohamed Saad.  Email: Engsaad_aly@hotmail.com  Occupation: Web Developer In IT Big Dig. PHP Course Info@ITBigDig.com
  3. 3. Contents • PHP Array Functions o while - loop o do...while o for - loop o foreach PHP Course Info@ITBigDig.com
  4. 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. 5. while - loops Syntax o while (condition) { code to be executed; } PHP Course Info@ITBigDig.com
  6. 6. <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
  7. 7. For Loop Syntax o for (Counter; condition; increment) { code to be executed; } PHP Course Info@ITBigDig.com
  8. 8. <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
  9. 9. Insert number and you will get textboxes equals that number
  10. 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> <?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 if else{echo "You entered invalid value";} }//end of empty($_POST) ?> <input type="submit" name="submit" value="Go"/> </form> </body> </html> Copy Code For_loop.php PHP Course Info@ITBigDig.com
  11. 11. do...while Statement Syntax o do { code to be executed; } while (condition); PHP Course Info@ITBigDig.com
  12. 12. 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.
  13. 13. <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 8 unset($value); print_r($x); ?> </pre> Copy Code foreeach.php PHP Course Info@ITBigDig.com
  14. 14. This will not work <?php foreach (array(1, 2, 3, 4) as &$value) { $value = $value * 2; } ?> We have to create variable first. Info@ITBigDig.com
  15. 15. We hope You enjoy This Tutorial. For any Suggestions Please Email Us Info@ITBigDig.com PHP Course Info@ITBigDig.com End Loops

×