SlideShare a Scribd company logo
© Copyright 2017 Hidaya Institute of Science and Technology
Looping & Branching
© Copyright 2017 Hidaya Institute of Science and Technology
• PHP scripts are a series of instructions in a file.
• PHP begins at the top of the file and executes each instruction, in
order, as it comes to it.
• However, some scripts need to be more complicated. You may
want your script to display one page to new customers and a
different page to existing customers.
• We need our code to be smarter, we need to start asking
questions, if the bank balance is positive calculate interest if
negative charge a penalty.
• Or you may need to display a list of phone numbers by executing
a single echo statement repeatedly, once for each phone number.
Controlling the Flow of the Script
© Copyright 2017 Hidaya Institute of Science and Technology
• PHP provides two types of complex
statements that enable you to perform tasks
like this — tasks that change the order in
which statements are executed:
• Conditional statements
• Looping statements
Complex Statement
© Copyright 2017 Hidaya Institute of Science and Technology
• Conditions are expressions that PHP tests or
evaluates to see whether they are true or
false.
• Conditions are used in complex statements to
determine whether or not a block of simple
statements should be executed.
• To set up conditions, you compare values.
Setting Up Conditions
© Copyright 2017 Hidaya Institute of Science and Technology
• Are two values equal?
• Is one value larger or smaller than another?
• Does a string match a pattern?
Setting Up Conditions
© Copyright 2017 Hidaya Institute of Science and Technology
Conditional Statement
 Conditional statements are the set of commands used to
perform different actions based on different conditions.
 In PHP we have the following conditional statements:
• If
• Else
• Else if
• Switch
© Copyright 2017 Hidaya Institute of Science and Technology
If Statement
 If structure is used for conditional execution of code segment.
 If something is true, then do something.
 Syntax:
if (expr)
{
Statements
}
© Copyright 2017 Hidaya Institute of Science and Technology
If Statement
 <?php
if ($c > $d)
{
echo "c is bigger than d";
}
?>
In the above example, only if the condition "$c>$d" is true,
the message "c is bigger than d" is displayed
© Copyright 2017 Hidaya Institute of Science and Technology
CONDITIONAL CODE
If( ) {
}
condition
// your additional code goes here
// . . .
© Copyright 2017 Hidaya Institute of Science and Technology
TERMINOLOGY
parentheses
brackets
braces
( )
{ }
[ ]
© Copyright 2017 Hidaya Institute of Science and Technology
CONDITIONAL CODE
If( ) {
}
condition
echo “it’s true!”;
// . . .
$a < 50
$b > 20
true or false?
$c == 99
$c === 99
$d != 100 Code block
© Copyright 2017 Hidaya Institute of Science and Technology
EQUALITY
If( $a == $b ){
// execute this code
}
© Copyright 2017 Hidaya Institute of Science and Technology
ASSIGNMENT INSTEAD OF EQUALITY
$a = 5;
$b = 10;
If( $a = $b ){
// always true!
}
© Copyright 2017 Hidaya Institute of Science and Technology
OPERATOR WITH =
= assignment
== equality
===strict equality
© Copyright 2017 Hidaya Institute of Science and Technology
COMPARISON
If( $a == $b ){ …
If( $a != $b ){ …
If( $a === $b ){ …
If( $a !== $b ){ …
If( $a > $b ){ …
If( $a < $b ){ …
If( $a >= $b ){ …
If( $a <= $b ){ …
© Copyright 2017 Hidaya Institute of Science and Technology
Using comparison operators
© Copyright 2017 Hidaya Institute of Science and Technology
&&
LOGICAL AND / OR
If( $a === $b $c === $d){…
If( $a === $b $c === $d){…
If( ($a > $b) && ($c > $d) ){…
and
or
||
© Copyright 2017 Hidaya Institute of Science and Technology
Else Statement
 If you want to execute some code if a condition is true and
another code if a condition is false, use the if....else
statement.
 Syntax
•if (condition)
code to be executed if condition is true;
•else
code to be executed if condition is false;
© Copyright 2017 Hidaya Institute of Science and Technology
Else Statement
 <?php
$c = 10;
$d = 20;
if ($c > $d) {
echo "C is bigger than d";
}
else {
echo "D is bigger than c";
}
?>
Result: D is bigger than C
In the above example in the if the condition "$c>$d" is true then the
message "C is bigger than D" is displayed, else the message "D is bigger than
C" is displayed.

More Related Content

PPTX
Working With Variables-[LAA] PHP Lj.pptx
PDF
Fuzzy Matching to the Rescue
PDF
This is a chapter about arrays and vectors in c++
PPTX
Learn PHP Basics
PPTX
Chapter 6 - More conditionals and loops
PDF
AWS Pricing Tutorial | AWS Certification Training | AWS Tutorial | Edureka
PDF
Big Data Engineer Skills and Job Description | Edureka
PDF
Fuzzy Matching on Apache Spark with Jennifer Shin
Working With Variables-[LAA] PHP Lj.pptx
Fuzzy Matching to the Rescue
This is a chapter about arrays and vectors in c++
Learn PHP Basics
Chapter 6 - More conditionals and loops
AWS Pricing Tutorial | AWS Certification Training | AWS Tutorial | Edureka
Big Data Engineer Skills and Job Description | Edureka
Fuzzy Matching on Apache Spark with Jennifer Shin

Recently uploaded (20)

PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Nekopoi APK 2025 free lastest update
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
history of c programming in notes for students .pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Digital Strategies for Manufacturing Companies
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
assetexplorer- product-overview - presentation
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Odoo POS Development Services by CandidRoot Solutions
Nekopoi APK 2025 free lastest update
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
top salesforce developer skills in 2025.pdf
history of c programming in notes for students .pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
How to Choose the Right IT Partner for Your Business in Malaysia
Upgrade and Innovation Strategies for SAP ERP Customers
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Digital Strategies for Manufacturing Companies
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Designing Intelligence for the Shop Floor.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Understanding Forklifts - TECH EHS Solution
assetexplorer- product-overview - presentation
Ad
Ad

Conditional Code (Day-1) Looping & Condition

  • 1. © Copyright 2017 Hidaya Institute of Science and Technology Looping & Branching
  • 2. © Copyright 2017 Hidaya Institute of Science and Technology • PHP scripts are a series of instructions in a file. • PHP begins at the top of the file and executes each instruction, in order, as it comes to it. • However, some scripts need to be more complicated. You may want your script to display one page to new customers and a different page to existing customers. • We need our code to be smarter, we need to start asking questions, if the bank balance is positive calculate interest if negative charge a penalty. • Or you may need to display a list of phone numbers by executing a single echo statement repeatedly, once for each phone number. Controlling the Flow of the Script
  • 3. © Copyright 2017 Hidaya Institute of Science and Technology • PHP provides two types of complex statements that enable you to perform tasks like this — tasks that change the order in which statements are executed: • Conditional statements • Looping statements Complex Statement
  • 4. © Copyright 2017 Hidaya Institute of Science and Technology • Conditions are expressions that PHP tests or evaluates to see whether they are true or false. • Conditions are used in complex statements to determine whether or not a block of simple statements should be executed. • To set up conditions, you compare values. Setting Up Conditions
  • 5. © Copyright 2017 Hidaya Institute of Science and Technology • Are two values equal? • Is one value larger or smaller than another? • Does a string match a pattern? Setting Up Conditions
  • 6. © Copyright 2017 Hidaya Institute of Science and Technology Conditional Statement  Conditional statements are the set of commands used to perform different actions based on different conditions.  In PHP we have the following conditional statements: • If • Else • Else if • Switch
  • 7. © Copyright 2017 Hidaya Institute of Science and Technology If Statement  If structure is used for conditional execution of code segment.  If something is true, then do something.  Syntax: if (expr) { Statements }
  • 8. © Copyright 2017 Hidaya Institute of Science and Technology If Statement  <?php if ($c > $d) { echo "c is bigger than d"; } ?> In the above example, only if the condition "$c>$d" is true, the message "c is bigger than d" is displayed
  • 9. © Copyright 2017 Hidaya Institute of Science and Technology CONDITIONAL CODE If( ) { } condition // your additional code goes here // . . .
  • 10. © Copyright 2017 Hidaya Institute of Science and Technology TERMINOLOGY parentheses brackets braces ( ) { } [ ]
  • 11. © Copyright 2017 Hidaya Institute of Science and Technology CONDITIONAL CODE If( ) { } condition echo “it’s true!”; // . . . $a < 50 $b > 20 true or false? $c == 99 $c === 99 $d != 100 Code block
  • 12. © Copyright 2017 Hidaya Institute of Science and Technology EQUALITY If( $a == $b ){ // execute this code }
  • 13. © Copyright 2017 Hidaya Institute of Science and Technology ASSIGNMENT INSTEAD OF EQUALITY $a = 5; $b = 10; If( $a = $b ){ // always true! }
  • 14. © Copyright 2017 Hidaya Institute of Science and Technology OPERATOR WITH = = assignment == equality ===strict equality
  • 15. © Copyright 2017 Hidaya Institute of Science and Technology COMPARISON If( $a == $b ){ … If( $a != $b ){ … If( $a === $b ){ … If( $a !== $b ){ … If( $a > $b ){ … If( $a < $b ){ … If( $a >= $b ){ … If( $a <= $b ){ …
  • 16. © Copyright 2017 Hidaya Institute of Science and Technology Using comparison operators
  • 17. © Copyright 2017 Hidaya Institute of Science and Technology && LOGICAL AND / OR If( $a === $b $c === $d){… If( $a === $b $c === $d){… If( ($a > $b) && ($c > $d) ){… and or ||
  • 18. © Copyright 2017 Hidaya Institute of Science and Technology Else Statement  If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.  Syntax •if (condition) code to be executed if condition is true; •else code to be executed if condition is false;
  • 19. © Copyright 2017 Hidaya Institute of Science and Technology Else Statement  <?php $c = 10; $d = 20; if ($c > $d) { echo "C is bigger than d"; } else { echo "D is bigger than c"; } ?> Result: D is bigger than C In the above example in the if the condition "$c>$d" is true then the message "C is bigger than D" is displayed, else the message "D is bigger than C" is displayed.