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

PPT
Introduction to php
PPTX
03loop conditional statements
PPTX
php programming.pptx
PPTX
Web Application Development using PHP Chapter 2
PDF
Web app development_php_05
PPT
Chapter 03 conditional statements
PPTX
advancing in php programming part four.pptx
PPTX
If Statements for PHP
Introduction to php
03loop conditional statements
php programming.pptx
Web Application Development using PHP Chapter 2
Web app development_php_05
Chapter 03 conditional statements
advancing in php programming part four.pptx
If Statements for PHP

Similar to Conditional Code (Day-1) Looping & Condition (20)

PPTX
Learn PHP Basics
PPSX
DIWE - Fundamentals of PHP
PPTX
Php & my sql
PDF
PHP and MySQL : Server Side Scripting For Web Development
PPTX
Unit-1 PHP Basic1 of the understanding of php.pptx
PPT
Class 2 - Introduction to PHP
PPTX
Chapter 5Internet Programming one PHP.pptx
DOC
PHP MATERIAL
PPTX
Conditional Statementfinal PHP 02
PDF
PHP-Part2
PPTX
PHP Basics
ODP
PPT
Prersentation
PPTX
Php basics
ODP
PHP Basic
PPT
PHP - Introduction to PHP - Mazenet Solution
PPTX
Php intro by sami kz
PPTX
Intro to php
PPT
Php mysql
PPTX
Php.ppt
Learn PHP Basics
DIWE - Fundamentals of PHP
Php & my sql
PHP and MySQL : Server Side Scripting For Web Development
Unit-1 PHP Basic1 of the understanding of php.pptx
Class 2 - Introduction to PHP
Chapter 5Internet Programming one PHP.pptx
PHP MATERIAL
Conditional Statementfinal PHP 02
PHP-Part2
PHP Basics
Prersentation
Php basics
PHP Basic
PHP - Introduction to PHP - Mazenet Solution
Php intro by sami kz
Intro to php
Php mysql
Php.ppt
Ad

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Cost to Outsource Software Development in 2025
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Complete Guide to Website Development in Malaysia for SMEs
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Salesforce Agentforce AI Implementation.pdf
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
assetexplorer- product-overview - presentation
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
Download FL Studio Crack Latest version 2025 ?
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
Nekopoi APK 2025 free lastest update
Cost to Outsource Software Development in 2025
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
17 Powerful Integrations Your Next-Gen MLM Software Needs
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Complete Guide to Website Development in Malaysia for SMEs
CHAPTER 2 - PM Management and IT Context
Designing Intelligence for the Shop Floor.pdf
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Salesforce Agentforce AI Implementation.pdf
Patient Appointment Booking in Odoo with online payment
Odoo Companies in India – Driving Business Transformation.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
assetexplorer- product-overview - presentation
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Download FL Studio Crack Latest version 2025 ?
Advanced SystemCare Ultimate Crack + Portable (2025)
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.