PHP Conditional Statements
PHP Conditional Statements are used to perform selected statements based on a certain condition. For example, suppose you want to execute some part of code when the variable $x value is 10.
- if statement – to execute some part of code when only one condition is true.
- if…else statement – to execute some part of code when a condition is true and execute another part of code when the condition is false.
- if…elseif…else statement – to implement the complex conditional logic you can use if inside if or if inside else.
PHP if Statement
if Statement is used to execute some part of code when only one condition is true.
if Statement Syntax
<!DOCTYPE html> <html> <head> </head> <body> <?php if(condition) { code is execute when condition is true; } ?> </body> </html>
Example 1 – if Statement Syntax and Code Example
<!DOCTYPE html> <html> <head> </head> <body> <?php $x = 20; if($x>10) { echo "The variable $x is greater than 10."; } ?> </body> </html>
In the above code, we describe that how you can use if co l statement.
Here we declare a variable $x of value 20 and condition is that if the value of $x is greater than 10 then print the value of variable $x is greater than 10.
PHP if — else Statement
if….else Statement is used to execute some part of code when a condition is true and execute another part of code when the condition is false.
if else Statement Syntax
<!DOCTYPE html> <html> <head> </head> <body> <?php if(condition) { code is execute when condition is true; } else { code is execute when condition is false; } ?> </body> </html>
Example 2 – if else Statement Application and Code Example
<!DOCTYPE html> <html> <head> </head> <body> <?php $age = 10; if($age>18) { echo "You are eligible for vote."; } else { echo "You are not eligible for vote."; } ?> </body> </html>
In the above code, we describe how you can use if….else statement.
Here we declare a variable $age of value 10 and condition is that if the value of $x is greater than 18 then print “You are eligible for vote.” otherwise print “You are not eligible for vote.”.
PHP if — elseif — else Shorthand Statement
if….elseif….else Statement is used to implement the complex conditional logic you can use if inside if or if inside else.
if elseif else Statement Syntax
<!DOCTYPE html> <html> <head> </head> <body> <?php if(condition) { code is execute when this condition is true; } elseif(condition) { code is execute when this condition is true; } else { code is execute when all conditions is false; } ?> </body> </html>
Caution: if….elseif….else Statement is a shorthand statement of the nested if or if inside a if or if inside else.
Example 3 – if elseif else shorthand Application and Code Example
<!DOCTYPE html> <html> <head> </head> <body> <?php $x = 10; $y = 10; if($x>$y) { echo "The value of variable $x is greater than variable $y."; } elseif($x==10) { echo "The value of variable $x is equal to the variable $y."; } else { echo "The value of variable $x is smaller than variable $y."; } ?> </body> </html>
In the above code, we describe how you can use if….elseif….else conditional statement.
Here we declare a variable 2 variable $x and $y of values 10 and conditions are that if the value of $x is greater than the value of $y than print “The value of variable $x is greater than variable $y.”
If the value of variable $x and $y have same value than print “The value of variable $x is equal to the variable $y.” and if the value of variable $x is less than the value of variable $y then print “The value of variable $x is smaller than variable $y.”.