In this article, we will be learning about if, else, and if-else statements.
These are the most commonly used conditional statements and are one of the most important skills in programming.
Conditional Statements*
Conditional statements are used to execute different codes for different conditions. Often when you write code, you want different code to execute depending on different conditions. To do this, we use conditional statements. In PHP, we have the following conditional statements: if statement: execute some code if some condition is true else statement: if some condition(s) are not true, then execute this code. if-else statement: if the above condition(s) are not true, then execute this code switch statement: depending on the value of a variable, execute some specific code (this is one particular tricky). If Statement You use the if statement to make a decision. Syntax
if(condition)
{
//Code to execute if the condition is true
}
In the above, inside the bracket you have a condition to check for true or not; if the condition is true, execute the code inside the {}
squiggly.
Example
As an example, let’s say we want to write an if statement to make sure that $a
is not 0.
<?php
$a = 0;
if($a==0)
{
echo "the variable cannot be zero";
}
?>
Result
Again, please note that you are using the correct “==” operator. Remember that if you want to check whether something equals something else, you use the “==” sign; A single equal sign “=“ means assign in PHP. And inside of the squiggly brackets, you are free to write as much code as you want. There are no restrictions here on how much code you can execute inside. Else statement The above covers “I want some code to execute if some condition is true”. However, what if I also want some code to execute only if the condition is not true. In this case, we use the else statement. An else statement basically says if none of the if conditions are true, then please execute this code instead. For example:
<?php
if()
{}
else
{}
?>
So the above code will check whether $a
is equal to 0. If it is, it will display the statement “your variable cannot be equal to zero”. Otherwise, it will print out “good job”.
Else if Statement
Now we’ve learned how to create if statement as well as how to create else statements.
Yet we have a problem: if you only use if else statement, you only get two choices.
Either you meet the condition and then something happens or you don’t meet the condition and something else happens.
Yet what if we want to else test for more than one condition?
Let’s say we want to check the value of A and if $a
equals to 0, then execute code block A. However if $a
is between 1 and 20, execute code block B. And if $a
is greater than 20, execute code block C. And if $a
is none of the above, then execute code block “D”.
Example
<?php
$a=0;
if($a==0)
{
echo "good";
}
else if ($a >=1 && $a<=20)
{
echo "great";
}
else if($a>20)
{
Echo "excellent";
}
else
{
Echo "no good";
}
?>
Result
One small point to note is that there’s actually no difference between writing else if or else-if: they mean exactly the same thing.
Difference Between If and Else If So why might you want to write else if instead of just another if? The two are actually not identical, and using the wrong choice can affect how your program functions. If you use else if, then if one of the conditions matches, it does not check for any other conditions, the program exits the statement.
Example
<?php
$a=22;
if($a==22)
{
echo "the variable is equal to 22";
echo "<br>"
}
else if($a%2==0)
{
echo "the variable is an even number";
}
else
{
echo "the variable is neither equal to 22 or an event number";
}
?>
In the example above, we have set $a
equal to 22. But 22 is also an even number, so it also matches the second condition. However, your program isn’t going to keep checking for more conditions in the loop.
By contrast, if we had written the above example like this:
<?php
$a=22;
if($a==22)
{
echo "the variable is equal to 22";
echo "<br>";
}
else if($a%2==0)
{
echo "the variable is an even number";
}
else
{
echo "the variable is neither equal to 22 or an event number";
}
?>
These are two additional if statements, meaning that the second condition on whether $a
is even or not will be checked. Hence, that second block of code will execute, unlike in the first example.