0% found this document useful (0 votes)
35 views22 pages

Using Conditional Code

The document discusses different conditional statements in PHP including if, elseif, else, and switch statements. The if statement executes code if a condition is true. The elseif statement checks additional conditions if the first is false. The else statement executes code if all preceding conditions are false. The switch statement compares a variable to different values and executes the corresponding code block. It is used to avoid long if/elseif blocks. The document provides syntax examples to illustrate usage of each statement.
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)
35 views22 pages

Using Conditional Code

The document discusses different conditional statements in PHP including if, elseif, else, and switch statements. The if statement executes code if a condition is true. The elseif statement checks additional conditions if the first is false. The else statement executes code if all preceding conditions are false. The switch statement compares a variable to different values and executes the corresponding code block. It is used to avoid long if/elseif blocks. The document provides syntax examples to illustrate usage of each statement.
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/ 22

USING CONDITIONAL CODE

IN PHP

BY: PROF. RIAZ


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
If Statement

If structure is used for conditional execution of code


segment.
If something is true, then do something.
 Syntax:
if (expr)
{
Statements
}
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
CONDITIONAL CODE

If( condition ) {
// your additional code goes here
// . . .

}
TERMINOLOGY

( parentheses )

[ brackets ]

{ braces }
CONDITIONAL CODE

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

}
EQUALITY

If( $a == $b ){

// execute this code

}
ASSIGNMENT INSTEAD OF EQUALITY

$a = 5;
$b = 10;
If( $a = $b ){
// always true!
}
OPERATOR WITH =

= assignment
== equality
===strict equality
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 ){ …
LOGICAL AND / OR

If( $a === $b and


&& $c === $d){…
If( $a === $b or|| $c === $d){…
If( ($a > $b) && ($c > $d) ){…
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;
Else if Statement

If you want to execute some code if one of several


conditions are true use the else if statement.
else if statement is used as extension of "If" structure if the
condition fails then it executes another "If" condition to
execute the code segment under the "else if" statement.
 Syntax:
 if (condition)

code to be executed if condition is true;


 elseif (condition)

code to be executed if condition is true;


 else

code to be executed if condition is false;


Else if Statement

 <?php
$c = 10;
$d = 10;
if ($c > $d) {
echo "c is bigger than d";
}
elseif ($c==$d) {
echo "c is equal to d";
}
else {
echo "d is smaller than c"; } ?>
Result: c is equal to d
In the above example the if the condition "$c>$d" is true then the
message "c is bigger than d" is displayed, else the condition in the else if
that is "$c==$d" is evaluated if it is true the message "c is equal to d" is
displayed otherwise "d is smaller than c" is displayed
Switch Statement

The Switch case statement is used to compare a variable or


expression to different values based on which a set of code
is executed.
If you want to select one of many blocks of code to be
executed, use the Switch statement.
The switch statement is used to avoid long blocks of
if..elseif..else code.
Switch Statement

Syntax:
switch ( <variable> ) {
case this-value:
Code to execute if <variable> == this-value
break;
case that-value:
Code to execute if <variable> == that-value
break;...
default:
Code to execute if <variable> does not equal the
value following any of the cases
break;
}
Else if Statement

 <?php
$x=2;
switch ($x) {
case 1:
echo “Number 1”;
break;
case 2:
echo “Number 2”;
break;
default:
echo “No number between 1 and 3”;
}
?>
Switch Statement

The condition of a switch statement is a value. The case


says that if it has the value of whatever is after that case
then do whatever follows the colon.
Break is a keyword that breaks out of the code block,
usually surrounded by braces, which it is in. In this case,
break prevents the program from falling through and
executing the code in all the other case statements
Switch Statement

It isn’t legal to use case:


$a = 10;
$b = 10;
$c = 20;
like this: switch ( $a ) {
case $b:
/* Code */ break;
case $c:
/* Code */ break;
default:
/* Code */ break; }
Switch Statement

The default case is optional, but it is wise to include it as it


handles any unexpected cases. It can be useful to put some
kind of output to alert you to the code entering the default
case if you don’t expect it to.
Switch statements serve as a simple way to write long if
statements when the requirements are met.
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.

You might also like