PHP conditional statements
You can use PHP If and PHP Else if conditional statements in PHP to perform different actions. These actions are based on the logic you want to perform using your code and get the result of the action.
By using the conditional statements you can perform various tests in your code and you can out certain actions based on the test either true or false.
In real life, you make certain decisions and based on this decision you perform different actions. These decisions help you to get the desired results as the output. Before taking any decision you always think of conditions, if you perform this task if you will get this type of result.
Same as in real life you can perform the conditional operation using the below type of conditional statements:
- PHP If statement
- PHP If…else statements
- PHP If…elseif…else statements
PHP If statement
PHP If statement is the conditional statement you can use when you want to test one condition and execute the code when the test is true.
Syntax
1 2 3 4 5 |
<?php if(Condition){ Put your code here to execute if the condition is true. } ?> |
Let’s take a simple example. You have a variable $x and you only want to print the string “Hello Friends” when the variable value is 9. With the use of the PHP statement, you can perform this process automatically without manually doing this.
If you stuck with the HTML codes how to do perform this task. You have the option using the PHP coding and PHP if statement.
Example 1:
1 2 3 4 5 6 7 8 9 |
<?php $p = 8; $q = 1; $x = $p + $q; if($x == 9){ echo "Hello Friends!" } echo "Welcome to Tutorialdeep PHP Tutorial."; ?> |
Output
PHP If…else statements
PHP If…else statement is the conditional statement you can use when you want to execute one code if the condition is true and the other code if the condition fails.
Syntax
1 2 3 4 5 6 7 |
<?php if(Condition){ Put your code here to execute if the condition is true. }else{ Put your code here to execute if the condition is false. } ?> |
Let’s take a simple example. Suppose you have a variable $z and you want to print the string “I have the larger number” if the $z is greater than 10 and another string “I have the smaller number” when $z is not greater than 10. You can do so using the if…else statement in your code.
Example 2:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $x = 8; $y = 1; $z = $x + $y; if($z>10){ echo "I have the larger number"; }else{ echo "I have the smallest number"; } echo "and Welcome to Tutorialdeep PHP Tutorial."; ?> |
Output
PHP If…elseif…else statements
PHP If…elseif…else statement is the conditional statement you can use when you want to perform multiple tests. You can put more than two conditions using this conditional statement.
Syntax
1 2 3 4 5 6 7 8 9 |
<?php if(Condition){ Put your code here to execute if the condition is true. }elseif(Condition){ Put your code here to execute if this condition is true. }else{ Put your code here to execute if all conditions are false. } ?> |
Now, Suppose you have a variable $z and you want to print the string “The number is 10” if the $z is equal than 10 and another string “I have the greatest number” when $z is greater than 10. If both conditions are false you want to print “The number is smaller”. You can do so using the if…elseif…else statement in your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $x = 8; $y = 9; $z = $x + $y; if($z == 10){ echo "The number is 10"; }elseif($z>10){ echo "I have the greatest number"; }else{ echo "The number is smaller"; } echo "and Welcome to Tutorialdeep PHP Tutorial."; ?> |
Output
You must also read:
Resource