Experiment No-02
Experiment No-02
Write a PHP program to demonstrate the use of decision making control structures
a) If statement
b) If else statement
c) Nested if statement
d) Switch statement
Resources required:
Hardware Software
Computer System Any database tools such as XAMPP
Practical Significance:
Theoretical Background:
a) if statement
The if statement is used to execute a block of code only if the specified condition
evaluates to true.
Syntax:
if(condition)
{
// Code to be executed
}
b) if-else Statement:
If...else statement first checks the condition. If condition is true, then true statement
block is executed. If condition is false, then false statement block is executed.
Syntax:
if (condition)
{
// if TRUE then execute this code
}
else
{
// if FALSE then execute this code
}
c) Nested-if Statement:
Nested if statements mean an if block inside another if block. Nested if else statement
used when we have more than two conditions. It is also called if else if statement.
Syntax:
if(condition1)
{
// Code to be executed if condition1 is true
}
elseif(condition2)
{
// Code to be executed if the
condition1 is false and condition2 is true
}
else
{
// Code to be executed if both condition1 and condition2 are false
}
a) Switch Statement
The switch-case statement is an alternative to the if-elseif-else statement, which
does almost the same thing. The switch-case statement tests a variable against a
series of values until it finds a match, and then executes the block of code
corresponding to that match. The switch statement is used to avoid long blocks of
if..elseif..else code.
Syntax:
switch(n)
{
case statement1:
//code to be executed if n==statement1;
break;
case statement2:
//code to be executed if n==statement2;
break;
case statement3:
//code to be executed if n==statement3;
break;
case statement4:
//code to be executed if n==statement4;
break;
......
default:
//code to be executed if n != any case;
}
<?php
echo”<br><br><b>……………………if statement………….<b></br></br>’;
If($a%2==0)
echo”$a is even number”;
?>
<?php
echo”<br><br><b>…………..if else statement……………..<b></br>”;
$a=10;
$b=30;
echo”a=$a<br>b=$b<br>”;
if($a>$b)
echo”a is greater than b”;
else
echo”b is greater than a”;
?>
Program Code: Write a program to demonstrate the use of Nested-if statement.
<?php
$per=65;
If($per>= 75)
{
echo "Distinction";
}
else if($per <75 && $per>=60)
{
echo "First Class";
}
else if ($per <60 && $per >= 45)
{
echo "Second Class";
}
else if ($per <45 && $per >= 40)
{
echo "Pass Class";
}
else
{
echo "Sorry Fail";
}
?>
<?php
$ch=1;
$a=20;
$b=10;
switch ($ch)
{
case 1:
$c=$a + $b;
echo "Addition=".$c;
break;
case 2:
$c=$a - $b;
echo "Subtraction=".$c;
break;
case 3:
$c=$a * $b;
echo "Multiplication=". $c;
break;
case 4:
$c=$a / $b;
echo "Division=". $c;
break;
default:
echo "Wrong Choice";
}
?>
Output:
Addition=30
Practical related questions:
1. How we use if..else and elseif statement in PHP?
2. Difference between if…else and switch statement.
3. Difference between if…else and ternary operator.
4. Why break and continue statement used in php?