Hello, welcome to zhullyblog. If this is your first time here , please
Subscribe to Zhullyblog by Email to get latest tutorial.
PHP conditional statement
As we keep moving further , we would find out that there are certain situations where you need your code to perform a task based on a certain condition.
And that is what leads us to conditional statement.
What is a conditional statement?
A conditional statement is a statement that the output is based on the condition specified. Conditional statement helps you make decisions.
There are 4 conditional statement which are :
• The if statement
• The if….else statement
• The if…elseif….else statement
• The switch case statement
The If statement
The if statement is simple because it only display the output only when the condition is true.
SYNTAX
If (condition) {
//The statement
}
Let’s take an example
<?php
$num = 18;
if($age >= 10)
{
echo "Number is valid";
}
?>
The if else statement
The if else statement is used when there is need for an alternative choice. By alternate , I mean if the condition isn't true, the output displays the else statement.
Unlike the if statement that displays blank if the condition returns false.
SYNTAX
If (condition) {
//Code
} else {
//Code
}
Let’s take an example
<?php
$age = 18;
if($age <= 20)
{
echo "You cannot visit the link";
}
else
{
echo "You can please";
}
?>
Here is the result.
The If…elseif…else statement
This is used to combine multiple if…else statement.
SYNTAX
If(condition){
//Body of the code
}elseif{
//Body of the code
}else{
//Body of the code
}
Here is an example
<?php
$temp = 120;
if($temp < 60)
{
echo "You are falling sick";
}
elseif($temp > 150 && $temp < 50)
{
echo "You are burning up";
}
else
{
echo "This temperature is bad";
}
?>
Please share
Awesome
ReplyDelete