SlideShare a Scribd company logo
1 of 22
Download to read offline
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)

Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
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
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
php
phpphp
php
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Php array
Php arrayPhp array
Php array
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Uploading a file with php
Uploading a file with phpUploading a file with php
Uploading a file with php
 
Get method and post method
Get method and post methodGet method and post method
Get method and post method
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Php introduction
Php introductionPhp introduction
Php introduction
 
File Uploading in PHP
File Uploading in PHPFile Uploading in PHP
File Uploading in PHP
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 
Introduction to HTML5
Introduction to HTML5Introduction to HTML5
Introduction to HTML5
 
PHP
PHPPHP
PHP
 

Viewers also liked

Viewers also liked (6)

Programming Basics - array, loops, funcitons
Programming Basics - array, loops, funcitonsProgramming Basics - array, loops, funcitons
Programming Basics - array, loops, funcitons
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Php if else
Php if elsePhp if else
Php if else
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Ruby
RubyRuby
Ruby
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
 

Similar to PHP Loops and PHP Forms (20)

php
phpphp
php
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Php i-slides (2) (1)
Php i-slides (2) (1)Php i-slides (2) (1)
Php i-slides (2) (1)
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
php41.ppt
php41.pptphp41.ppt
php41.ppt
 
PHP InterLevel.ppt
PHP InterLevel.pptPHP InterLevel.ppt
PHP InterLevel.ppt
 
php-I-slides.ppt
php-I-slides.pptphp-I-slides.ppt
php-I-slides.ppt
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
PHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet SolutionPHP - Introduction to PHP - Mazenet Solution
PHP - Introduction to PHP - Mazenet Solution
 
Php
PhpPhp
Php
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
Php web development
Php web developmentPhp web development
Php web development
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_master
 
Php Basics
Php BasicsPhp Basics
Php Basics
 
PHP Comprehensive Overview
PHP Comprehensive OverviewPHP Comprehensive Overview
PHP Comprehensive Overview
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 

Recently uploaded

Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 

Recently uploaded (20)

Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 

PHP Loops and PHP Forms

  • 1. Lecturer: M.Zalmai “Rahmani” rahmani.zalmai@gmail.com 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