SlideShare a Scribd company logo
9
Most read
10
Most read
13
Most read
CN5109
WEB APPLICATION
DEVELOPMENT
Chapter 2
PHP – Conditional and Loop Statements
Conditional Statements
• Conditional statements are used to perform different actions
based on different conditions.
• It perform different computations or actions depending on
whether a programmer-specified boolean condition evaluates
to true or false.
Conditional Statements
• The following are conditional statements in PHP:
• if statement - executes some code if one condition is
true
• if...else statement - executes some code if a condition
is true and another code if that condition is false
• if...elseif....else statement - executes different codes
for more than two conditions
• switch statement - selects one of many blocks of code
to be executed
IF Statement
• The if statement executes some code if one condition is true.
Syntax:
if (condition) {
code to be executed if condition is true;
}
IF Statement
• Example:
<?php
$t = 15;
if ($t < 20) {
echo "Have a good day!";
}
?>
Output:
IF … ELSE Statement
• The if....else statement executes some code if a condition is
true and another code if that condition is false.
Syntax:
if (condition) {
code to be executed if condition is true;
}
else {
code to be executed if condition is false;
}
IF … ELSE Statement
• Example:
<?php
$t = 15;
if ($t < 20) {
echo "Have a good day!";
}
else {
echo "Have a good night!";
}
?>
Output:
IF … ELSEIF … ELSE Statement
• The if....elseif...else statement executes different codes for
more than two conditions.
Syntax:
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
IF … ELSEIF … ELSE Statement
• Example:
<?php
$t = 5;
if ($t < 10) {
echo "Have a good morning!";
} elseif ($t < 20) {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Output:
SWITCH Statement
• The switch statement is used to perform different actions
based on different conditions.
Syntax:
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
SWITCH Statement
• Example:
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
Output:
Quiz
a. What do CONDITIONAL STATEMENTS used for?
b. List down any four examples of CONDITIONAL STATEMENTS.
Quiz
Write a PHP code using Conditional Statement:
If AGE below 18, display message “You cannot enter.”
If MARKS more than 39, display message “You Passed!”
Else, display message “You Failed!”
Write one example using SWITCH Statement
Loop Statements
• A loop statement in programming performs predefined tasks
while or until a predetermined condition is met.
• The following looping statements in PHP:
• while - loops through a block of code as long as the
specified condition is true
• do...while - loops through a block of code once, and
then repeats the loop as long as the 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
WHILE Loop
• The while loop executes a block of code as long as the
specified condition is true.
Syntax:
while (condition is true) {
code to be executed;
}
WHILE Loop
Example:
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Output:
DO … WHILE Loop
• The do...while loop will always execute the block of code once,
it will then check the condition, and repeat the loop while the
specified condition is true.
Syntax:
do {
code to be executed;
} while (condition is true);
DO … WHILE Loop
Example:
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Output:
FOR Loop
• The for loop is used when you know in advance how many
times the script should run.
• Parameters:
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it evaluates to
TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
• increment counter: Increases the loop counter value
Syntax:
for (init counter; test counter; increment counter) {
code to be executed;
} condition is true);
FOR Loop
Example:
<?php
for ($x = 20; $x <= 15; $x++) {
echo "The number is: $x <br>";
}
Echo “End loop”;
?>
Output:
FOREACH Loop
• The foreach loop works only on arrays, and is used to loop
through each key/value pair in an array.
Syntax:
foreach ($array as $value) {
code to be executed;
}
FOREACH Loop
Example:
<?php
$numbers = array(1,45,3,90);
foreach ($numbers as $value) {
echo "$value <br>";
}
?>
Output:
Quiz
a. Define LOOP STATEMENT.
b. Explain the difference between WHILE Statement and DO …
WHILE Statement.
c. Write a PHP code using FOR Loop Statement to display
number 10 to 30.

More Related Content

What's hot (20)

Chapter 03 conditional statements
Chapter 03 conditional statements
Dhani Ahmad
 
Loops in c
Loops in c
RekhaBudhwar
 
Iteration
Iteration
Liam Dunphy
 
For Loop
For Loop
Ghaffar Khan
 
5 c control statements looping
5 c control statements looping
MomenMostafa
 
C++ loop
C++ loop
Khelan Ameen
 
Do...while loop structure
Do...while loop structure
Jd Mercado
 
Loops in c
Loops in c
baabtra.com - No. 1 supplier of quality freshers
 
Presentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 
Loop c++
Loop c++
Mood Mood
 
Looping in c++
Looping in c++
deekshagopaliya
 
Loops in java script
Loops in java script
Ravi Bhadauria
 
Lecture05(control structure part ii)
Lecture05(control structure part ii)
Dhaka University of Engineering & Technology(DUET)
 
170120107074 looping statements and nesting of loop statements
170120107074 looping statements and nesting of loop statements
harsh kothari
 
Loops in c language
Loops in c language
tanmaymodi4
 
Looping
Looping
Kulachi Hansraj Model School Ashok Vihar
 
Perl exceptions lightning talk
Perl exceptions lightning talk
Peter Edwards
 
Iteration Statement in C++
Iteration Statement in C++
Jaypee Institute of Information Technology
 
Loops
Loops
abdulmanan366
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 

Similar to Web Application Development using PHP Chapter 2 (20)

PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
rehna9
 
PHP-Part2
PHP-Part2
Ahmed Saihood
 
PHP Lecture 02.pptx PHP Lecture 02 .pptx
PHP Lecture 02.pptx PHP Lecture 02 .pptx
shahgohar1
 
Decision and looping examples with php (WT)
Decision and looping examples with php (WT)
kunjan shah
 
Lesson 6 php if...else...elseif statements
Lesson 6 php if...else...elseif statements
MLG College of Learning, Inc
 
Intro to php
Intro to php
Ahmed Farag
 
Php.ppt
Php.ppt
Nidhi mishra
 
All about decision making statements in php
All about decision making statements in php
Programing Code Example
 
Php Basics Iterations, looping
Php Basics Iterations, looping
Muthuganesh S
 
advancing in php programming part four.pptx
advancing in php programming part four.pptx
KisakyeDennis
 
Php modul-2
Php modul-2
Kristophorus Hadiono
 
PHP Lecture 2 - Conditional Statement, Loop
PHP Lecture 2 - Conditional Statement, Loop
Al-Mamun Sarkar
 
PHP - Web Development
PHP - Web Development
Niladri Karmakar
 
introduction to php web programming 2024.ppt
introduction to php web programming 2024.ppt
idaaryanie
 
CPAP.com Introduction To Coding: Part 2
CPAP.com Introduction To Coding: Part 2
johnnygoodman
 
PPT 19.pptx
PPT 19.pptx
DrRavneetSingh
 
PHP Training in Ambala ! Batra Computer Centre
PHP Training in Ambala ! Batra Computer Centre
jatin batra
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web Development
Edureka!
 
unit 1.pptx
unit 1.pptx
adityathote3
 
Php(report)
Php(report)
Yhannah
 
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
rehna9
 
PHP Lecture 02.pptx PHP Lecture 02 .pptx
PHP Lecture 02.pptx PHP Lecture 02 .pptx
shahgohar1
 
Decision and looping examples with php (WT)
Decision and looping examples with php (WT)
kunjan shah
 
All about decision making statements in php
All about decision making statements in php
Programing Code Example
 
Php Basics Iterations, looping
Php Basics Iterations, looping
Muthuganesh S
 
advancing in php programming part four.pptx
advancing in php programming part four.pptx
KisakyeDennis
 
PHP Lecture 2 - Conditional Statement, Loop
PHP Lecture 2 - Conditional Statement, Loop
Al-Mamun Sarkar
 
introduction to php web programming 2024.ppt
introduction to php web programming 2024.ppt
idaaryanie
 
CPAP.com Introduction To Coding: Part 2
CPAP.com Introduction To Coding: Part 2
johnnygoodman
 
PHP Training in Ambala ! Batra Computer Centre
PHP Training in Ambala ! Batra Computer Centre
jatin batra
 
PHP and MySQL : Server Side Scripting For Web Development
PHP and MySQL : Server Side Scripting For Web Development
Edureka!
 
Php(report)
Php(report)
Yhannah
 
Ad

More from Mohd Harris Ahmad Jaal (20)

Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
Mohd Harris Ahmad Jaal
 
Ad

Recently uploaded (20)

THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Vikas Bansal Himachal Pradesh: A Visionary Transforming Himachal’s Educationa...
Himalayan Group of Professional Institutions (HGPI)
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
GEOGRAPHY-Study Material [ Class 10th] .pdf
GEOGRAPHY-Study Material [ Class 10th] .pdf
SHERAZ AHMAD LONE
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 

Web Application Development using PHP Chapter 2

  • 1. CN5109 WEB APPLICATION DEVELOPMENT Chapter 2 PHP – Conditional and Loop Statements
  • 2. Conditional Statements • Conditional statements are used to perform different actions based on different conditions. • It perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
  • 3. Conditional Statements • The following are conditional statements in PHP: • if statement - executes some code if one condition is true • if...else statement - executes some code if a condition is true and another code if that condition is false • if...elseif....else statement - executes different codes for more than two conditions • switch statement - selects one of many blocks of code to be executed
  • 4. IF Statement • The if statement executes some code if one condition is true. Syntax: if (condition) { code to be executed if condition is true; }
  • 5. IF Statement • Example: <?php $t = 15; if ($t < 20) { echo "Have a good day!"; } ?> Output:
  • 6. IF … ELSE Statement • The if....else statement executes some code if a condition is true and another code if that condition is false. Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • 7. IF … ELSE Statement • Example: <?php $t = 15; if ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> Output:
  • 8. IF … ELSEIF … ELSE Statement • The if....elseif...else statement executes different codes for more than two conditions. Syntax: if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; }
  • 9. IF … ELSEIF … ELSE Statement • Example: <?php $t = 5; if ($t < 10) { echo "Have a good morning!"; } elseif ($t < 20) { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> Output:
  • 10. SWITCH Statement • The switch statement is used to perform different actions based on different conditions. Syntax: switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
  • 11. SWITCH Statement • Example: <?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?> Output:
  • 12. Quiz a. What do CONDITIONAL STATEMENTS used for? b. List down any four examples of CONDITIONAL STATEMENTS.
  • 13. Quiz Write a PHP code using Conditional Statement: If AGE below 18, display message “You cannot enter.” If MARKS more than 39, display message “You Passed!” Else, display message “You Failed!” Write one example using SWITCH Statement
  • 14. Loop Statements • A loop statement in programming performs predefined tasks while or until a predetermined condition is met. • The following looping statements in PHP: • while - loops through a block of code as long as the specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as the 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
  • 15. WHILE Loop • The while loop executes a block of code as long as the specified condition is true. Syntax: while (condition is true) { code to be executed; }
  • 16. WHILE Loop Example: <?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> Output:
  • 17. DO … WHILE Loop • The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Syntax: do { code to be executed; } while (condition is true);
  • 18. DO … WHILE Loop Example: <?php $x = 1; do { echo "The number is: $x <br>"; $x++; } while ($x <= 5); ?> Output:
  • 19. FOR Loop • The for loop is used when you know in advance how many times the script should run. • Parameters: • init counter: Initialize the loop counter value • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. • increment counter: Increases the loop counter value Syntax: for (init counter; test counter; increment counter) { code to be executed; } condition is true);
  • 20. FOR Loop Example: <?php for ($x = 20; $x <= 15; $x++) { echo "The number is: $x <br>"; } Echo “End loop”; ?> Output:
  • 21. FOREACH Loop • The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax: foreach ($array as $value) { code to be executed; }
  • 22. FOREACH Loop Example: <?php $numbers = array(1,45,3,90); foreach ($numbers as $value) { echo "$value <br>"; } ?> Output:
  • 23. Quiz a. Define LOOP STATEMENT. b. Explain the difference between WHILE Statement and DO … WHILE Statement. c. Write a PHP code using FOR Loop Statement to display number 10 to 30.