Conditional Statement in php
Conditional statement is used to perform different actions for different decisions in a specific program. Conditional statements are the set of commands used to perform different actions based on different conditions.
Here we will look at two structures: if...else and switch statements. Both perform in the same way the same task.
There are two way to use if else
1-if....else statement
Use this statement if you want to execute a set of code when a condition is true and another if the condition is false.
2-else if statement
Is used with the if...else statement to execute a set of code if one of several condition are true
Syntax:1
if(condition)
{
body of if
}
else
{
body of else
}
Rules:If condition is true then if body executed otherwise else body is excuted.
Example:1
<?php
$age=17;
if($age<18)
{
echo "you are under age";
}
else
{
echo "Good Luck";
}
?>
Output :you are under age
Syntax:2
if(condition)
{
if block
}
else if(condition)
{
second if block
}
else
{
another block
}
Example:2
<?php
$age=18;
if($age<18)
{
echo "you are under age";
}
else if($age<30)
{
echo "you are medium age";
}
else
{
echo "you are in proper age";
}
?>
Output :you are medium age
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.
Syntax:
switch (expression)
{
case label1:
//code to be executed if expression = label1;
break;
case label2:
//code to be executed if expression = label2;
break;
default:
//code to be executed if expression is different it mins no match found for any label
}
Rules to Excute:
A single expression (most often a variable) is evaluated once
The value of the expression is compared with the values for each case in the structure
If there is a match, the code associated with that case is executed
After a code is executed, break is used to stop the code from running into the next case
The default statement is used if none of the cases are true
Example:
<html>
<body>
<?php
$x=2;
Here we will look at two structures: if...else and switch statements. Both perform in the same way the same task.
There are two way to use if else
1-if....else statement
Use this statement if you want to execute a set of code when a condition is true and another if the condition is false.
2-else if statement
Is used with the if...else statement to execute a set of code if one of several condition are true
Syntax:1
if(condition)
{
body of if
}
else
{
body of else
}
Rules:If condition is true then if body executed otherwise else body is excuted.
Example:1
<?php
$age=17;
if($age<18)
{
echo "you are under age";
}
else
{
echo "Good Luck";
}
?>
Output :you are under age
Syntax:2
if(condition)
{
if block
}
else if(condition)
{
second if block
}
else
{
another block
}
Example:2
<?php
$age=18;
if($age<18)
{
echo "you are under age";
}
else if($age<30)
{
echo "you are medium age";
}
else
{
echo "you are in proper age";
}
?>
Output :you are medium age
Switch Statement
Syntax:
switch (expression)
{
case label1:
//code to be executed if expression = label1;
break;
case label2:
//code to be executed if expression = label2;
break;
default:
//code to be executed if expression is different it mins no match found for any label
}
Rules to Excute:
Example:
<html>
<body>
<?php
$x=2;
switch ($x) { case 1: echo "you are choosing Number 1"; break; case 2: echo "you are choosing Number 2"; break; case 3: echo "you are choosing Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html>
Output:you are choosing Number 2