SlideShare a Scribd company logo
1 of 20
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 Info@ITBigDig.com
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 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.
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"/>
<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
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"/>
<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
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 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
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 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 8
unset($value);
print_r($x);
?>
</pre>
Copy Code
foreeach.php
PHP Course Info@ITBigDig.com
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
We hope You enjoy This Tutorial.
For any Suggestions Please Email Us
Info@ITBigDig.com
PHP Course Info@ITBigDig.com
End
Loops

More Related Content

What's hot

JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
Jussi Pohjolainen
 

What's hot (20)

Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
PHP slides
PHP slidesPHP slides
PHP slides
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
PHP - Introduction to PHP Forms
PHP - Introduction to PHP FormsPHP - Introduction to PHP Forms
PHP - Introduction to PHP Forms
 
Php basics
Php basicsPhp basics
Php basics
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
php
phpphp
php
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
Javascript
JavascriptJavascript
Javascript
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
JavaScript - Part-1
JavaScript - Part-1JavaScript - Part-1
JavaScript - Part-1
 

Similar to Loops PHP 04

Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
H K
 

Similar to Loops PHP 04 (20)

PHP-04-Forms.ppt
PHP-04-Forms.pptPHP-04-Forms.ppt
PHP-04-Forms.ppt
 
HTML5 workshop, forms
HTML5 workshop, formsHTML5 workshop, forms
HTML5 workshop, forms
 
Drupal Form API 101 (PHP) - DrupalCamp LA 2012
Drupal Form API 101 (PHP) - DrupalCamp LA 2012Drupal Form API 101 (PHP) - DrupalCamp LA 2012
Drupal Form API 101 (PHP) - DrupalCamp LA 2012
 
03-forms.ppt.pptx
03-forms.ppt.pptx03-forms.ppt.pptx
03-forms.ppt.pptx
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
 
php part 2
php part 2php part 2
php part 2
 
Java Script
Java ScriptJava Script
Java Script
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validation
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
phptut2
phptut2phptut2
phptut2
 
phptut2
phptut2phptut2
phptut2
 
phptut2
phptut2phptut2
phptut2
 
phptut2
phptut2phptut2
phptut2
 
Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4
 
PHP-Part1
PHP-Part1PHP-Part1
PHP-Part1
 
PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array.
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-Kjaer
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
PHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginnersPHP complete reference with database concepts for beginners
PHP complete reference with database concepts for beginners
 

More from Spy Seat

Notify progresscontrol
Notify progresscontrolNotify progresscontrol
Notify progresscontrol
Spy Seat
 
Listbox+checkedlistbox
Listbox+checkedlistboxListbox+checkedlistbox
Listbox+checkedlistbox
Spy Seat
 
If then vs select case
If then vs select caseIf then vs select case
If then vs select case
Spy Seat
 
If then vb2010
If then vb2010If then vb2010
If then vb2010
Spy Seat
 
Date & time picker
Date & time pickerDate & time picker
Date & time picker
Spy Seat
 

More from Spy Seat (20)

Kazdoura & Luciano Jan – Aug 2016 Cost Analysis
Kazdoura & Luciano  Jan – Aug 2016 Cost AnalysisKazdoura & Luciano  Jan – Aug 2016 Cost Analysis
Kazdoura & Luciano Jan – Aug 2016 Cost Analysis
 
Software Design
Software DesignSoftware Design
Software Design
 
Visual Basic 6 Data Base
Visual Basic 6 Data BaseVisual Basic 6 Data Base
Visual Basic 6 Data Base
 
Visual Basic ADO
Visual Basic ADOVisual Basic ADO
Visual Basic ADO
 
Visual basic 6
Visual basic 6Visual basic 6
Visual basic 6
 
Create Contacts program with VB.Net
Create Contacts program with VB.NetCreate Contacts program with VB.Net
Create Contacts program with VB.Net
 
Create Your first website
Create Your first websiteCreate Your first website
Create Your first website
 
How Computer work
How Computer workHow Computer work
How Computer work
 
visual basic 6 Add Merlin
visual basic 6 Add Merlin visual basic 6 Add Merlin
visual basic 6 Add Merlin
 
My Bachelor project slides
My Bachelor project slides My Bachelor project slides
My Bachelor project slides
 
Difference between asp and php
Difference between asp and phpDifference between asp and php
Difference between asp and php
 
Error handling
Error handlingError handling
Error handling
 
I/O PHP Files and classes
I/O PHP Files and classesI/O PHP Files and classes
I/O PHP Files and classes
 
Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topics
 
Notify progresscontrol
Notify progresscontrolNotify progresscontrol
Notify progresscontrol
 
Listbox+checkedlistbox
Listbox+checkedlistboxListbox+checkedlistbox
Listbox+checkedlistbox
 
If then vs select case
If then vs select caseIf then vs select case
If then vs select case
 
If then vb2010
If then vb2010If then vb2010
If then vb2010
 
For next
For nextFor next
For next
 
Date & time picker
Date & time pickerDate & time picker
Date & time picker
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Loops PHP 04

  • 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.
  • 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
  • 11. Insert number and you will get textboxes equals that number
  • 12.
  • 13. <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
  • 14. do...while Statement Syntax o do { code to be executed; } while (condition); PHP Course Info@ITBigDig.com
  • 15.
  • 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.
  • 17.
  • 18. <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
  • 19. 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
  • 20. We hope You enjoy This Tutorial. For any Suggestions Please Email Us Info@ITBigDig.com PHP Course Info@ITBigDig.com End Loops