0% found this document useful (0 votes)
11 views19 pages

Conditional Code (Day-1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Conditional Code (Day-1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Looping & Branching

© Copyright 2017 Hidaya Institute of Science and Technology


Controlling the Flow of the Script

• 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.

© Copyright 2017 Hidaya Institute of Science and Technology


Complex Statement

• 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

© Copyright 2017 Hidaya Institute of Science and Technology


Setting Up Conditions

• 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.

© Copyright 2017 Hidaya Institute of Science and Technology


Setting Up Conditions

• Are two values equal?


• Is one value larger or smaller than another?
• Does a string match a pattern?

© 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

true or false?
If( $d
$a
$b
$c )===
<{20
condition
==
>
!= 50
100
99 99 Code block
echo “it’s true!”;
// . . .

© 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 and


&&$c === $d){…
If( $a === $b or|| $c === $d){…
If( ($a > $b) && ($c > $d) ){…

© 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.
© Copyright 2017 Hidaya Institute of Science and Technology

You might also like