Sri Krishna Arts And Science College
Department of ICT
Course Title: PHP & MYSQL
Course Code: 19CSS22
Class : III BSc – Computer Technology “A”
Google Class room Code: 7cxe5v3
Online Material
Lecture 4: Flow Control-IF & SWITCH
Facilitator
[Link]
Assistant Professor
Department of Computer Technology
SKASC
Objective
if statement
switch statement
Text Books
1. Rasmus Lerdorf, Kevin Tatroe, Bob Kaehms, RicMcGredy
(2020), Programming PHP, O’REILLY(SPD), 4th Edition.
2. Vikram Vaswani, 2017, “PHP A Beginners Guide”, Tata
McGrawHill
3. Lee Babin, Nathan A. Good, Frank M. Kromann, Jon
Stephens (2013), “PHP 5Recipes, A problem solution
approach”, après
Conditional statements
Topic: Conditional statements
Reference: Dani Krossing Channel
Conditional statements
Topic: Condional statements
Reference: HEaRt Drive Tutorial
Conditional Statements
Conditional statements are used to perform different actions
based on different conditions.
Very often when you write code, you want to perform different
actions for different conditions. You can use conditional
statements in your code to do this.
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true
and another code if that condition is false
Conditional Statements
if...elseif...else statement - executes different codes for more
than two conditions
switch statement - selects one of many blocks of code to be
executed
if Statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
"Have a good day!" if the current
Example
time (HOUR) is less than 20:
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
if Statement
if...else Statement
if...else statement executes some code if a condition is true and
another code if that condition is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
if...else Statement
if...else Statement-Example
<?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
The if...elseif...else statement executes different codes for more
than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition
is true;
} else {
code to be executed if all conditions are false; }
if...elseif...else Statement
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
Write a program to check even number.(Number entered by
user)
<?php
if(isset($_GET['save']))
{
if($_GET['n']%2==0)
echo $_GET['n']." is even number";
} }
?>
Example
Write a program to check even number.(Number entered by
user)
<form method="get">
Enter Your number<input type="text" name="n"/><hr/>
<input type="submit" value="check number" name="save"/>
</form> Enter Your number
10
</body> 10 is even number
Fill up the coding
Output "Hello World" if $a is greater than $b.
$a = 50;
$b = 10;
__ (______> ______)
echo "Hello World";
}
Fill up the coding
Output "Hello World" if $a is NOT equal to $b.
$a = 50;
$b = 10;
__ (______> ______)____
echo "Hello World";
}
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:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
Important points to be noticed about switch case
The default is an optional statement.
Even it is not important, that default must always be the last
statement.
There can be only one default in a switch statement.
More than one default may lead to a Fatal error.
Each case can have a break statement, which is used to
terminate the sequence of statement.
The break statement is optional to use in switch.
If break is not used, all the statements will execute after
finding matched case value.
Important points to be noticed about switch case
PHP allows you to use number, character, string, as well as
functions in switch expression.
Nesting of switch statements is allowed, but it makes the
program more complex and less readable.
You can use semicolon (;) instead of colon (:). It will not
generate any error.
PHP Switch Flowchart
How it works
First we have a single expression n (most often a variable),
that is evaluated once.
The value of the expression is then compared with the values
for each case in the structure.
If there is a match, the block of code associated with that case
is executed.
Use break to prevent the code from running into the next case
automatically.
The default statement is used if no match is found.
Example
<?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