Conditional structures are used in your code to make decisions.
PHP if condition
If statement is used when one condition is true. (Only one statement ) If you want to use a condition for one state, you can use it if condition. To complete one task, you can use only if condition. If the condition works when the condition is true. If the condition is not true, the statement does not work for your code. Use if the condition is true for only one state.
Syntax of PHP If condition
The syntax below helps you understand the if condition.
if (condition) {
Statement (Code will be executed when the condition is true);
}
In the example below, we will declare the value of a variable and create a condition. If the condition is true, then some value will be returned, and if the condition is false, then no value will be returned.
When the condition is true
<?php
$x = 20;
if($x < "40")
{
echo "X is greater";
}?>
Run
When the condition is not true
<?php
$x = 10;
if ($x >"40") {
echo "X is greater";
}?>
output-
There will be nothing to display as output.
As you can see from the examples above. In the first example, if the condition is true, the code is executed. In the second example, if the condition is false, nothing is displayed as output, if the condition works on a single state, which is a true statement. You can use an if-else condition or if-else statement to handle the two states, true and false.
The if-else Statements
An if-else statement has two states. The first is true and the second is false. Just like there is only one state in the if condition. If the statement executes code if the condition is true. If conditions become false, then if the statement will not produce any output. For the solution of this problem, we use the if-else statement. In an if-else statement, there are two types of states if one is true, a statement(code) will be executed, and if the condition is false, then another statement will be executed.
In simple words, when the condition is true, a code will be executed, and when the condition is false, another code will be executed.
Syntax of if-else statements
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
The following example will output " Y is Greater" if the condition is true, otherwise(else) it will output "X is Greater ":
Example
<?php
$x=30;
$y=20;
if ($x<$y)
echo "Y is greater";
else
echo "X is Greater ";
?>
Run
PHP -The if...elseif...else Statement
If you need to use two or more if-else conditions, then you can use the if..elseif...else Statement. In if...elseif...else statement, we can create more than one condition. However, in the if-else statements, we are able to create only one condition. If the condition is true, then the code will be executed, and if the condition is false, then the other code will be executed. But in nested if-else statements, you can use more than one condition.
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
The below example will output "Have a nice Friday " if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise, it will output "Have a good day.
Example
<?php
$day="Fri";
if ($day=="Fri")
echo "Have a nice Friday ";
elseif ($day=="Sun")
echo "Have a happy Sunday";
else
echo "Have a good day";
?>
Run
In the nested if-else, if a condition is matched, then the compiler exits the block.