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 (20)

PHP variables
PHP  variablesPHP  variables
PHP variables
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Php string function
Php string function Php string function
Php string function
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Form Validation in JavaScript
Form Validation in JavaScriptForm Validation in JavaScript
Form Validation in JavaScript
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Php
PhpPhp
Php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 

Similar to Loops PHP 04

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

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 AnalysisSpy Seat
 
Software Design
Software DesignSoftware Design
Software DesignSpy Seat
 
Visual Basic 6 Data Base
Visual Basic 6 Data BaseVisual Basic 6 Data Base
Visual Basic 6 Data BaseSpy Seat
 
Visual Basic ADO
Visual Basic ADOVisual Basic ADO
Visual Basic ADOSpy Seat
 
Visual basic 6
Visual basic 6Visual basic 6
Visual basic 6Spy Seat
 
Create Contacts program with VB.Net
Create Contacts program with VB.NetCreate Contacts program with VB.Net
Create Contacts program with VB.NetSpy Seat
 
Create Your first website
Create Your first websiteCreate Your first website
Create Your first websiteSpy Seat
 
How Computer work
How Computer workHow Computer work
How Computer workSpy Seat
 
visual basic 6 Add Merlin
visual basic 6 Add Merlin visual basic 6 Add Merlin
visual basic 6 Add Merlin Spy Seat
 
My Bachelor project slides
My Bachelor project slides My Bachelor project slides
My Bachelor project slides Spy Seat
 
Difference between asp and php
Difference between asp and phpDifference between asp and php
Difference between asp and phpSpy Seat
 
Error handling
Error handlingError handling
Error handlingSpy Seat
 
I/O PHP Files and classes
I/O PHP Files and classesI/O PHP Files and classes
I/O PHP Files and classesSpy Seat
 
Php session 3 Important topics
Php session 3 Important topicsPhp session 3 Important topics
Php session 3 Important topicsSpy Seat
 
Notify progresscontrol
Notify progresscontrolNotify progresscontrol
Notify progresscontrolSpy Seat
 
Listbox+checkedlistbox
Listbox+checkedlistboxListbox+checkedlistbox
Listbox+checkedlistboxSpy Seat
 
If then vs select case
If then vs select caseIf then vs select case
If then vs select caseSpy Seat
 
If then vb2010
If then vb2010If then vb2010
If then vb2010Spy Seat
 
Date & time picker
Date & time pickerDate & time picker
Date & time pickerSpy 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

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
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.pptxAreebaZafar22
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Recently uploaded (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
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
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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