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)

Sending emails through PHP
Sending emails through PHPSending emails through PHP
Sending emails through PHP
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Looping statement
Looping statementLooping statement
Looping statement
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Php forms
Php formsPhp forms
Php forms
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Lesson 5 php operators
Lesson 5   php operatorsLesson 5   php operators
Lesson 5 php operators
 
Chap 4 PHP.pdf
Chap 4 PHP.pdfChap 4 PHP.pdf
Chap 4 PHP.pdf
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 

Similar to Loops PHP 04

HTML5 workshop, forms
HTML5 workshop, formsHTML5 workshop, forms
HTML5 workshop, formsRobert Nyman
 
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 2012Chris Charlton
 
03-forms.ppt.pptx
03-forms.ppt.pptx03-forms.ppt.pptx
03-forms.ppt.pptxThắng It
 
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 5Salvatore Iaconesi
 
Html basics 11 form validation
Html basics 11 form validationHtml basics 11 form validation
Html basics 11 form validationH K
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit universityMandakini Kumari
 
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/4DEVCON
 
PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. PHP Arrays - indexed and associative array.
PHP Arrays - indexed and associative array. wahidullah mudaser
 
Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Web Development Course: PHP lecture 2
Web Development Course: PHP lecture 2Gheyath M. Othman
 
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-KjaerCOMMON Europe
 

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
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
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
 

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

ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 

Recently uploaded (20)

ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.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