SlideShare a Scribd company logo
2
Most read
3
Most read
6
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)

PPT
PHP variables
Siddique Ibrahim
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PPTX
Operators php
Chandni Pm
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PPT
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
PPTX
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
PPT
Php forms
Anne Lee
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
PPT
Java interfaces
Raja Sekhar
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Virtual base class
Tech_MX
 
PPTX
Php string function
Ravi Bhadauria
 
PPTX
Print input-presentation
Martin McBride
 
PPT
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
PPTX
Data types in php
ilakkiya
 
PPTX
File handling in Python
Megha V
 
PHP variables
Siddique Ibrahim
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Operators php
Chandni Pm
 
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Operators and expressions in C++
Neeru Mittal
 
Statements and Conditions in PHP
Maruf Abdullah (Rion)
 
Php forms
Anne Lee
 
Control Statements in Java
Niloy Saha
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
Java interfaces
Raja Sekhar
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Virtual base class
Tech_MX
 
Php string function
Ravi Bhadauria
 
Print input-presentation
Martin McBride
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Data types in php
ilakkiya
 
File handling in Python
Megha V
 

Viewers also liked (6)

PPTX
Programming Basics - array, loops, funcitons
Trivuz ত্রিভুজ
 
PPTX
Loops PHP 04
mohamedsaad24
 
PPSX
Php if else
argusacademy
 
PPT
Ruby Basics
SHC
 
ODP
Ruby
Aizat Faiz
 
PDF
Ruby on Rails for beginners
Vysakh Sreenivasan
 
Programming Basics - array, loops, funcitons
Trivuz ত্রিভুজ
 
Loops PHP 04
mohamedsaad24
 
Php if else
argusacademy
 
Ruby Basics
SHC
 
Ruby on Rails for beginners
Vysakh Sreenivasan
 
Ad

Similar to PHP Loops and PHP Forms (20)

PPT
introduction to php web programming 2024.ppt
idaaryanie
 
PPT
Php i-slides
ravi18011991
 
PPT
Php i-slides
zalatarunk
 
PPT
Php i-slides (2) (1)
ravi18011991
 
PPT
Php i-slides
Abu Bakar
 
PPT
php41.ppt
Nishant804733
 
PPT
php-I-slides.ppt
SsewankamboErma
 
PPT
PHP InterLevel.ppt
NBACriteria2SICET
 
PDF
Programming in PHP Course Material BCA 6th Semester
SanthiNivas
 
PDF
1336333055 php tutorial_from_beginner_to_master
jeeva indra
 
PPT
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
PDF
Php tutorial(w3schools)
Arjun Shanka
 
PDF
Php tutorialw3schools
rasool noorpour
 
PPTX
Basic of PHP
Nisa Soomro
 
PDF
Php web development
Ramesh Gupta
 
PDF
Php tutorial from_beginner_to_master
PrinceGuru MS
 
PPSX
PHP Comprehensive Overview
Mohamed Loey
 
introduction to php web programming 2024.ppt
idaaryanie
 
Php i-slides
ravi18011991
 
Php i-slides
zalatarunk
 
Php i-slides (2) (1)
ravi18011991
 
Php i-slides
Abu Bakar
 
php41.ppt
Nishant804733
 
php-I-slides.ppt
SsewankamboErma
 
PHP InterLevel.ppt
NBACriteria2SICET
 
Programming in PHP Course Material BCA 6th Semester
SanthiNivas
 
1336333055 php tutorial_from_beginner_to_master
jeeva indra
 
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
Php tutorial(w3schools)
Arjun Shanka
 
Php tutorialw3schools
rasool noorpour
 
Basic of PHP
Nisa Soomro
 
Php web development
Ramesh Gupta
 
Php tutorial from_beginner_to_master
PrinceGuru MS
 
PHP Comprehensive Overview
Mohamed Loey
 
Ad

Recently uploaded (20)

PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
PDF
Dealing with JSON in the relational world
Andres Almiray
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PDF
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
 
Automated Test Case Repair Using Language Models
Lionel Briand
 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
 
Dealing with JSON in the relational world
Andres Almiray
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
How DeepSeek Beats ChatGPT: Cost Comparison and Key Differences
sumitpurohit810
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
For my supp to finally picking supp that work
necas19388
 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 

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