If, Else, Elseif are called the Conditional statements that are used for performing different actions according to different conditions.
If, Else, Elseif are called the Conditional statements that are used for performing different actions according to different conditions.3.9. Operators; If, Else, Elseif; Loops
Conditional Statements
If, Else, Elseif are called the Conditional statements that are used for performing different actions according to different conditions. Many times programmers may have to apply different conditions to perform different results according to condition, at that time, these operations are being used.
In PHP we have the following conditional statements:
3.9.1. if statement
if statement is used in the statement for executing some code only when it the specified condition is true. It ignores when the condition is false, e.g.
if (condition) {
// code in here to execute if the condition is true
}
Here it is in action. First we set the variable
<?php
$Studentname = “Ajay”;
if ($Studentname == “Ajay”) {
print (“Ajay is the monitor of the class!”);
}
?>
Please note that:
if ($Studentname = “Ajay”)
and
if ($Studentname == “Ajay”)
are not the same thing, the first will always be true as it sets $ Studentname to Ajay.
3.9.2. if...else statement
This statement is used in the statement to check the condition is either true or false. If..else statement execute the code in both conditions, e.g.
It works like this:
if (condition) {
// code in here to execute if the condition is true
} else {
// code in here to execute if the condition is not true
}
Here it is in motion:
<?php
$ Studentname = “Ajay”;
if ($Studentname == “Ajay”) {
print (“Ajay is the monitor of the class”);
} else {
print (“He is not Ajay!”);
}
?>
In the above example, the first set of { and } belong to the “if,” the second { and } belong to the “else.” If the condition is true, the output print will be Ajay is the monitor of the class and if false, it will print the next print statement ‘He is not Ajay'.
3.9.3. if...elseif....else statement
This statement is used for executing codes one among the several blocks of else. In this statement, we can use if...elseif....else statement the code this statement to select one of several blocks of code to be executed.
This statement functions in the block like this:
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;
For example:
<?php
$y=Year(“y”);
if ($y== “2000”)
Print “This is a leap year!”;
elseif ($y== “2001”)
echo “Year 2000 is not a leap year!”;
else
echo “Have a nice Year!”;
?>
The following example will output “This is a leap year!” if the current year is 2000, and “Year 2000 is not a leap year!” if the current year is 2001. Otherwise it will output “Have a nice year!”.
3.9.4. Switch statement
Switch statement is used for executing one block at a time among several blocks. The program directly jumps on that block for which the command is given.