Your SlideShare is downloading. ×
Loops PHP 04
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Loops PHP 04

1,243

Published on

PHP course Lecture 4 …

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,243
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP Course PHP Course Info@ITBigDig.com Loops
  • 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. <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. For Loop Syntax o for (Counter; condition; increment) { code to be executed; } PHP Course Info@ITBigDig.com
  • 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. Insert number and you will get textboxes equals that number
  • 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. do...while Statement Syntax o do { code to be executed; } while (condition); PHP Course Info@ITBigDig.com
  • 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. <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. 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. We hope You enjoy This Tutorial. For any Suggestions Please Email Us Info@ITBigDig.com PHP Course Info@ITBigDig.com End Loops

×