LECTURE 4-Flow Control-IF & Switch
LECTURE 4-Flow Control-IF & Switch
LECTURE 4-Flow Control-IF & Switch
Department of ICT
Online Material
Lecture 4: Flow Control-IF & SWITCH
Facilitator
B.Hemalatha
Assistant Professor
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
"Have a good day!" if the current
} time (HOUR) is less than 20:and
?> "Have a good night!" otherwise:
if...elseif...else Statement
<?php
$t = date("H");
"Have a good morning!" if the
if ($t < "10") { current time is less than 10, and
"Have a good day!" if the current
echo "Have a good morning!"; time is less than 20. Otherwise it
will output "Have a good night!":
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
Example
if(isset($_GET['save']))
{
if($_GET['n']%2==0)
} }
?>
Example
$a = 50;
$b = 10;
__ (______> ______)
}
Fill up the coding
Output "Hello World" if $a is NOT equal to $b.
$a = 50;
$b = 10;
__ (______> ______)____
}
Fill up the coding
utput "Yes" if $a is equal to $b, otherwise output "No".
$a = 50;
$b = 10;
______($a == $b) {
echo "Yes";
_______
echo "No";
}
Fill up the coding
Output "1" if $a is equal to $b, print "2" if $a is greater than $b,
otherwise output "3".
$a = 50;
$b = 10;
_______($a == $b) {
echo "1";
}
______($a > $b) {
echo "2";
}
______ {
echo "3"; }
switch Statement
Is used to perform different actions based on different
conditions.
To select one of many blocks of code to be executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
switch Statement
case label3:
break;
...
default:
}
Important points to be noticed about switch case
The default is an optional statement.
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
Example
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?> Your favorite color is red!
Fill up the coding
------------ ($color)
{
----------"red":
echo "Hello";
break;
--------"green":
echo "Welcome";
break;
}
Fill up the coding
switch ($color) {
case "red":
echo "Hello";
break;
case "green":
echo "Welcome";
break;
_______
echo "Neither";
}
Do It Yourself
Click
THANK YOU