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)

PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PPTX
HTML Forms
Ravinder Kamboj
 
PPTX
Php string function
Ravi Bhadauria
 
PPTX
Looping statement
ilakkiya
 
PPT
Php forms
Anne Lee
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PPT
Oops concepts in php
CPD INDIA
 
PPTX
Data types in php
ilakkiya
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PDF
Php array
Nikul Shah
 
ODP
Introduction of Html/css/js
Knoldus Inc.
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PDF
javascript objects
Vijay Kalyan
 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PPT
PHP - Introduction to PHP Forms
Vibrant Technologies & Computers
 
PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PPTX
Stacks in c++
Vineeta Garg
 
PPTX
Introduction to php
Taha Malampatti
 
PHP FUNCTIONS
Zeeshan Ahmed
 
HTML Forms
Ravinder Kamboj
 
Php string function
Ravi Bhadauria
 
Looping statement
ilakkiya
 
Php forms
Anne Lee
 
Oops concepts in php
CPD INDIA
 
Data types in php
ilakkiya
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Php array
Nikul Shah
 
Introduction of Html/css/js
Knoldus Inc.
 
javascript objects
Vijay Kalyan
 
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PHP - Introduction to PHP Forms
Vibrant Technologies & Computers
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Stacks in c++
Vineeta Garg
 
Introduction to php
Taha Malampatti
 

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
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
PDF
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
PPT
introduction to php web programming 2024.ppt
idaaryanie
 
PDF
Phpbasics
PrinceGuru MS
 
PPTX
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
PPT
Web development
Seerat Bakhtawar
 
PPTX
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
PDF
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 
PPTX
php basics
Anmol Paul
 
PPTX
Php training in chandigarh
CBitss Technologies
 
PPT
PHP and MySQL
Sanketkumar Biswas
 
PDF
Lecture14-Introduction to PHP-coding.pdf
IotenergyWater
 
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
PDF
WT_PHP_PART1.pdf
HambardeAtharva
 
PPT
MIND sweeping introduction to PHP
BUDNET
 
PPT
Php mysql
Alebachew Zewdu
 
PPT
Php i-slides
zalatarunk
 
PHP - Introduction to PHP - Mazenet Solution
Mazenetsolution
 
PHP Lec 2.pdfsssssssssssssssssssssssssss
ksjawyyy
 
introduction to php web programming 2024.ppt
idaaryanie
 
Phpbasics
PrinceGuru MS
 
Quick beginner to Lower-Advanced guide/tutorial in PHP
Sanju Sony Kurian
 
Web development
Seerat Bakhtawar
 
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
Lecture2_IntroductionToPHP_Spring2023.pdf
ShaimaaMohamedGalal
 
php basics
Anmol Paul
 
Php training in chandigarh
CBitss Technologies
 
PHP and MySQL
Sanketkumar Biswas
 
Lecture14-Introduction to PHP-coding.pdf
IotenergyWater
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
WT_PHP_PART1.pdf
HambardeAtharva
 
MIND sweeping introduction to PHP
BUDNET
 
Php mysql
Alebachew Zewdu
 
Php i-slides
zalatarunk
 
Ad

Recently uploaded (20)

PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 

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