Last modified on October 14th, 2020
If else in PHP
There exist some conditions in “If Else Statements” to perform some specific tasks. All actions/functions totally depend on the conditions specified in “If Else Statement”. The condition can be True or False.
Example of If Else Statement
For example, in the figure, we can see the following real-life examples;
- If we obey God, We will get Heaven, Otherwise/Else we will get Hell.
- If someone fails in the exams, he will weep, Otherwise/Else he will be happy with a smile.
- If you work smart and work hard, you will get more money, Otherwise/Else you will get less money.
Let’s see an example of if else statement.
We want to write a program in PHP in which we will get a number, and then we will check that number is even number or odd number.
Get a number and divide the number with 2, if the remainder is zero, then the number is even, otherwise a number is an odd number.
Suppose number is 4, then 4 % 2 = 0, so 4 is even number. Similarly, if the number is 3, then 3/2 = 1, so the number is not even.
If else Example in PHP
if($number % 2 == 0)
echo "number is even";
echo "number is odd";