PHP Tutorial – If else Statements
In this PHP programming tutorial we will take a look at the “if else statement” (the PHP “switch statement“ can be found here.) Conditional statements (such as “if else” or “switch”) are used to alter the flow of a program if a specific test condition is true.
Before we start make sure that you know the comparison operators (or sometimes also called Boolean operators) of PHP. Click the following link to read more about PHP Comparison Operators.
Different Kinds of Conditional Statements
Often you want to make different actions based on different conditions in your program. This is where conditional statements come in.
The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If the specific statement is not true, execute this instruction.
In the PHP language we have the following conditional statements:
- if statement – you can use this statement to execute some code if some specific condition is true.
- if…else statement – you can use this statement to execute some code if the conditions is true. If the condition is not true do something different. (Other words: Do something if condition is set. If it’s not, execute the other piece of code.)
- if…elseif…else statement – can be used to select one of several blocks of code (using different conditions) to be executed.
- switch – can be used to execute one of many blocks of code to be executed.
Let’s look at some “if then else” examples of the statements described above.
The if statement
As we said before; you can use the “if” statement to execute some code if some specific condition is true. An example:
<?php
$var=date(“D”);
if($var==”Mon”) echo “Wake up! You have to go to work.”;
?>
The example above ask for the current day using the function date(“D”) and put the result in $var. In the if statement we compare $var with “Mon”. If they are equal the echo is executed, with the result that ‘Wake up! You have to go to work.’ (without the ‘’) is printed on the screen. Note: for all other days in the week nothing is printed, so this example will only work on Monday!
The if…else statement
As we said before; you can use the “if…else” statement to execute some code if the conditions is true. If the condition is not true do something different (Do something if condition is set. If it’s not, execute the other piece of code.)
An example of the “if…else” statement:
<?php
$var=1;
if($var == 2) {
echo “The if statement is true”;
} else {
echo “The if statement is not true”;
}
?>
Note: using curly brackets you define a block of code. For instance you can add a second echo line to the first and/or second block.
This example will compare 1 with 2. This is not true, so the second piece of echo will be executed.
The if…elseif…else Statement
As we said before; you can can use the “if…elseif…else” statement to select one of several blocks of code (using different conditions) to be executed. Let’s look at an example:
<?php
$var=1;
$var2=1;
if($var == 2) {
echo “The if statement is true”;
} elseif($var == $var2) {
echo “The second if statement is true”;
} else {
echo “None of the first if statement where true”;
}
?>
That’s all for this tutorial next time we will look at the switch statement.
Very useful example for the freshers
good one!
Brilliant! Very well explained!
good