In the previous PHP tutorial on Introduction to PHP, you learnt about the basics of PHP. In this tutorial, you will learn how to use IF-ELSE in PHP programs.
Like other programming languages, IF-ELSE statements can be used in PHP also. These statements are used for comparison of two or more than two values. Comparison operators like ==, !=,>,< etc. can be used for such purpose.
Let’s write a program using if-else:
<?php $x=20; if($x=="10") { echo "hello"; } else { echo "bye"; } ?>
“If” is followed by conditional statement ($x= =“10”) which checks whether value of x (as defined before) is equal to 10 or not. “= =” used here, is the equal to operator used between two operands. After the conditional statement, the desired output is written under ‘{‘ curly braces. Here, “hello” word will be displayed if value of x is equal to 10. But we have assigned it 10 as the value. As this is a false statement, the control is forwarded to the else statement which has the code to display “bye”. The control of the statement goes to else when the “if” statement is found incorrect.
Thus, the output is bye which can be obtained by typing http:/localhost/file_name.php where the file_name is the name of the file by which it was saved.
ELSEIF
If there are multiple statements to be checked, we use elseif. See a program how elseif can be used:
<?php $x=20; $y=30; if($x=="10") { echo "hello"; } elseif ($y==“30”) { echo “sorry”; } else { echo "bye"; } ?>
As explained above if the “if” statement is found to be false, the control of the flow goes to the next statement, which here is elseif. It will check whether the value of y is 30 or not. “Sorry” will get displayed as the value assigned to y before is 30. Thus the output is 30. Other than “= =” operator, there are few more operators like <, > etc that can be used for comparison.
Let’s take a quick look on these:
<?php $x=20; $y=30; if($y>$x) { echo "y is greater"; } ?>
Greater than operator compares the two operands to check if first operand ($y) is greater than the second operator ($x) and displays the statement mentioned as echo.
If there are more than two conditional statements, logical operators can be used.
&& (and), || (or), ! (not) are three such operators. Eg:
<?php $x=20; $y=30; if("$x>$y" || "$y<30") { echo "y is below 30"; } ?>
Here, OR (||) operator is used which gives the output if any of the two statements is correct. Here, since “$y<30” is true, the control passes to the next line even if “$x>$y” is false. Opposite to this, AND (&&) operator considers the conditional statement as true only if both the statements are correct. And the logical operator, AND (!) is used as “not equal” condition, opposite to (= =) equal operator.
SWITCH
This is required when there are number of possible statements to be checked. The example below shows how it is used.
<?php $x=100; switch ($x) { case 10: echo "It is not 100"; break; case 20: echo "It is not 100"; break; case 30: echo "It is not 100"; break; case 50: echo "It is not 100"; break; case 100: echo "It is 100"; break; default: echo "Try again"; } ?>
Switch will find for that case where the value of x is 100; if it does not find that value, default case will be executed and “Try again” will be displayed. Since there is a case 100 in these case statements, the output will be “It is 100”.
Now that you know how to use IF-ELSE in PHP, continue with the next PHP tutorial on how to use FOR and FOREACH loop in PHP. Also, if you have any queries, please leave your comment below.
1 Comment
You write very well