SlideShare a Scribd company logo
2
PHP Loops
PHP Loops
 Loops execute a block of code a specified number of times, or while a
specified condition is true.
 Often when you write code, you want the same block of code to run over
and over again in a row. Instead of adding several almost equal lines in a
script we can use loops to perform a task like this.
 In PHP, we have the following looping statements:
• while - 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 - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an array
Most read
3
PHP Loops
The while Loop
The while loop executes a block of code while a condition is true.
Most read
10
PHP Loops
Example
The following example demonstrates a loop that will print the values of the
given array:
Most read
Lecturer:
M.Zalmai “Rahmani”
rahmani.zalmai@gmail.com
Web Programming II
Lecture 04
PHP Basic & Loops
Azma Institute
Database Department
PHP Loops
PHP Loops
 Loops execute a block of code a specified number of times, or while a
specified condition is true.
 Often when you write code, you want the same block of code to run over
and over again in a row. Instead of adding several almost equal lines in a
script we can use loops to perform a task like this.
 In PHP, we have the following looping statements:
• while - 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 - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an array
PHP Loops
The while Loop
The while loop executes a block of code while a condition is true.
PHP Loops
Example
The example below defines a loop that starts with i=1.The loop will continue
to run as long as i is less than, or equal to 5. i will increase by 1 each time the
loop runs:
PHP Loops
The do...while Statement
The do...while statement will always execute the block of code once, it will
then check the condition, and repeat the loop while the condition is true.
PHP LoopsExample
The example below defines a loop that starts with i=1. It will then increment i
with 1, and write some output.Then the condition is checked, and the loop
will continue to run as long as i is less than, or equal to 5:
PHP Loops
The for Loop
The for loop is used when you know in advance how many times the script
should run.
Parameters:
init: Mostly used to set a counter (but can be any code to be executed once at the beginning
of the loop)
condition: Evaluated for each loop iteration. If it evaluates toTRUE, the loop continues. If it
evaluates to FALSE, the loop ends.
increment: Mostly used to increment a counter (but can be any code to be executed at the
end of the loop)
Note: Each of the parameters above can be empty, or have multiple expressions (separated
by commas).
PHP Loops
Example
The example below defines a loop that starts with i=1.The loop will continue
to run as long as i is less than, or equal to 5. i will increase by 1 each time the
loop runs:
PHP Loops
The foreach Loop
The foreach loop is used to loop through arrays.
For every loop iteration, the value of the current array element is assigned to
$value (and the array pointer is moved by one) - so on the next loop
iteration, you'll be looking at the next array value.
PHP Loops
Example
The following example demonstrates a loop that will print the values of the
given array:
PHP Forms
PHP Forms and User Input
PHP Forms
PHP Form Handling
 The PHP $_GET and $_POST variables are used to retrieve information
from forms, like user input.
 The most important thing to notice when dealing with HTML forms and
PHP is that any form element in an HTML page will automatically be
available to your PHP scripts.
 The example below contains an HTML form with two input fields and a
submit button:
PHP Forms
PHP Form Handling
 When a user fills out the form above and click on the submit button, the
form data is sent to a PHP file, called "welcome.php":
"welcome.php" looks like this:
PHP Forms
PHP $_GET Function
 The built-in $_GET function is used to collect values in a form with
method="get".
 Information sent from a form with the GET method is visible to everyone
(it will be displayed in the browser's address bar) and has limits on the
amount of information to send.
PHP Forms
PHP $_GET Function
 When the user clicks the "Submit" button, the URL sent to the server
could look something like this:
PHP Forms
When to use method="get"?
 When using method="get" in HTML forms, all variable names and values
are displayed in the URL.
Note:This method should not be used when sending passwords or other
sensitive information!
 However, because the variables are displayed in the URL, it is possible to
bookmark the page.This can be useful in some cases.
Note:The get method is not suitable for very large variable values. It should
not be used with values exceeding 2000 characters.
PHP Forms
The $_POST Function
 The built-in $_POST function is used to collect values from a form sent
with method="post".
 Information sent from a form with the POST method is invisible to others
and has no limits on the amount of information to send.
Note: However, there is an 8 Mb max size for the POST method, by default
(can be changed by setting the post_max_size in the php.ini file).
PHP Forms
The $_POST Function
PHP Forms
When to use method="post"?
 Information sent from a form with the POST method is invisible to others
and has no limits on the amount of information to send.
 However, because the variables are not displayed in the URL, it is not
possible to bookmark the page.
The PHP $_REQUEST Function
 The PHP built-in $_REQUEST function contains the contents of both
$_GET, $_POST, and $_COOKIE.
 The $_REQUEST function can be used to collect form data sent with both
the GET and POST methods.
We will cover PHP functions in a separate lecture
www.w3schools.com
PHP  Loops and PHP Forms

More Related Content

What's hot (20)

Php array
Php arrayPhp array
Php array
Nikul Shah
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
Compare Infobase Limited
 
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
Zeeshan Ahmed
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Php forms
Php formsPhp forms
Php forms
Anne Lee
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
Tiji Thomas
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
Rajat Saxena
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
Nisa Soomro
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
mohamedsaad24
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
Ravinder Kamboj
 
PHP POWERPOINT SLIDES
PHP POWERPOINT SLIDESPHP POWERPOINT SLIDES
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlMysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
Aimal Miakhel
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 

Similar to PHP Loops and PHP Forms (20)

Php Basics
Php BasicsPhp Basics
Php Basics
Shaheed Udham Singh College of engg. n Tech.,Tangori,Mohali
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
PHP Lec 2.pdfsssssssssssssssssssssssssss
PHP Lec 2.pdfsssssssssssssssssssssssssssPHP Lec 2.pdfsssssssssssssssssssssssssss
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
introduction to php web programming 2024.ppt
introduction to php web programming 2024.pptintroduction to php web programming 2024.ppt
introduction to php web programming 2024.ppt
idaaryanie
 
php
phpphp
php
بلال الحمدان
 
Phpbasics
PhpbasicsPhpbasics
Phpbasics
PrinceGuru MS
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Quick beginner to Lower-Advanced guide/tutorial in PHPQuick beginner to Lower-Advanced guide/tutorial in PHP
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
Web development
Web developmentWeb development
Web development
Seerat Bakhtawar
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 
php basics
php basicsphp basics
php basics
Anmol Paul
 
Php training in chandigarh
Php  training in chandigarhPhp  training in chandigarh
Php training in chandigarh
CBitss Technologies
 
PHP and MySQL
PHP and MySQLPHP and MySQL
PHP and MySQL
Sanketkumar Biswas
 
Lecture14-Introduction to PHP-coding.pdf
Lecture14-Introduction to PHP-coding.pdfLecture14-Introduction to PHP-coding.pdf
Lecture14-Introduction to PHP-coding.pdf
IotenergyWater
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
HambardeAtharva
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
Dr. Ramkumar Lakshminarayanan
 
Php mysql
Php mysqlPhp mysql
Php mysql
Alebachew Zewdu
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
zalatarunk
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
PHP Lec 2.pdfsssssssssssssssssssssssssss
PHP Lec 2.pdfsssssssssssssssssssssssssssPHP Lec 2.pdfsssssssssssssssssssssssssss
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
introduction to php web programming 2024.ppt
introduction to php web programming 2024.pptintroduction to php web programming 2024.ppt
introduction to php web programming 2024.ppt
idaaryanie
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Quick beginner to Lower-Advanced guide/tutorial in PHPQuick beginner to Lower-Advanced guide/tutorial in PHP
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
Lecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdfLecture2_IntroductionToPHP_Spring2023.pdf
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 
Lecture14-Introduction to PHP-coding.pdf
Lecture14-Introduction to PHP-coding.pdfLecture14-Introduction to PHP-coding.pdf
Lecture14-Introduction to PHP-coding.pdf
IotenergyWater
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
Ad

Recently uploaded (20)

Top 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdfTop 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Trackobit
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Bonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdfBonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdf
Herond Labs
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
Software Testing & it’s types (DevOps)
Software  Testing & it’s  types (DevOps)Software  Testing & it’s  types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
Key AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence CompaniesKey AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence Companies
Mypcot Infotech
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdfTop 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Trackobit
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Bonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdfBonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdf
Herond Labs
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
Software Testing & it’s types (DevOps)
Software  Testing & it’s  types (DevOps)Software  Testing & it’s  types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
Key AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence CompaniesKey AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence Companies
Mypcot Infotech
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Ad

PHP Loops and PHP Forms

  • 1. Lecturer: M.Zalmai “Rahmani” [email protected] Web Programming II Lecture 04 PHP Basic & Loops Azma Institute Database Department
  • 2. PHP Loops PHP Loops  Loops execute a block of code a specified number of times, or while a specified condition is true.  Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.  In PHP, we have the following looping statements: • while - 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 - loops through a block of code a specified number of times • foreach - loops through a block of code for each element in an array
  • 3. PHP Loops The while Loop The while loop executes a block of code while a condition is true.
  • 4. PHP Loops Example The example below defines a loop that starts with i=1.The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
  • 5. PHP Loops The do...while Statement The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true.
  • 6. PHP LoopsExample The example below defines a loop that starts with i=1. It will then increment i with 1, and write some output.Then the condition is checked, and the loop will continue to run as long as i is less than, or equal to 5:
  • 7. PHP Loops The for Loop The for loop is used when you know in advance how many times the script should run. Parameters: init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop) condition: Evaluated for each loop iteration. If it evaluates toTRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop) Note: Each of the parameters above can be empty, or have multiple expressions (separated by commas).
  • 8. PHP Loops Example The example below defines a loop that starts with i=1.The loop will continue to run as long as i is less than, or equal to 5. i will increase by 1 each time the loop runs:
  • 9. PHP Loops The foreach Loop The foreach loop is used to loop through arrays. For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
  • 10. PHP Loops Example The following example demonstrates a loop that will print the values of the given array:
  • 11. PHP Forms PHP Forms and User Input
  • 12. PHP Forms PHP Form Handling  The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.  The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.  The example below contains an HTML form with two input fields and a submit button:
  • 13. PHP Forms PHP Form Handling  When a user fills out the form above and click on the submit button, the form data is sent to a PHP file, called "welcome.php": "welcome.php" looks like this:
  • 14. PHP Forms PHP $_GET Function  The built-in $_GET function is used to collect values in a form with method="get".  Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
  • 15. PHP Forms PHP $_GET Function  When the user clicks the "Submit" button, the URL sent to the server could look something like this:
  • 16. PHP Forms When to use method="get"?  When using method="get" in HTML forms, all variable names and values are displayed in the URL. Note:This method should not be used when sending passwords or other sensitive information!  However, because the variables are displayed in the URL, it is possible to bookmark the page.This can be useful in some cases. Note:The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.
  • 17. PHP Forms The $_POST Function  The built-in $_POST function is used to collect values from a form sent with method="post".  Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
  • 19. PHP Forms When to use method="post"?  Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.  However, because the variables are not displayed in the URL, it is not possible to bookmark the page. The PHP $_REQUEST Function  The PHP built-in $_REQUEST function contains the contents of both $_GET, $_POST, and $_COOKIE.  The $_REQUEST function can be used to collect form data sent with both the GET and POST methods.
  • 20. We will cover PHP functions in a separate lecture