Using Conditional Code
Using Conditional Code
IN PHP
<?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 ){
}
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
<?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
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
<?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.