Updated March 23, 2023
Introduction to PHP if Statement
PHP programming language has some basic features to achieve complex things if statement is one of them. This is one of the basic building blocks of any programming language. Whenever we talk about something conditional things we need to have the if-else to get the job done. Yes, you read it right, it can be followed by the else whenever required as per the business requirements. Using this if statement we can compare two or more things and on the basis of that result, we can perform further options. We cannot assume any application without using this if-else statement.
Syntax for PHP if Statement
There are various ways of using this if statements, we will see all with syntax and bit description about it.
1. Using a Single Line if Statement
Syntax:
if (expression)
statement
if is the keyword we can say in the PHP language like any other programming language. The expression is a conditional statement. The statement is just an instruction that will be executed when this if condition will be true. There is no need to use the braces for a single line of the statement.
2. Using Multiple Lines of if Statements
Syntax:
if (expression){
statement 1
statement 2
}
There is only one difference between this one and the above one is that we have braces in this. We can use these braces when we need to execute the multi-line statements.
3. Using if else Statements together
Syntax:
if (expression){
statement 1
statement 2
}
else{
statement 1
}
We can use the if and else together to easily tackle the business requirements. Else is nothing but it is just a case. The else section will be executed once the if section will be false. In case of our if, the expression is true else will not be executed.
4. Multiple if Statements
Syntax:
if (expression 1){
statement 1
statement 2
}
if (expression 2){
statement 3
statement 3
}
if (expression 3){
statement 4
}
We can also use the multiple if in our programming code or the application. This will help us to check or validate the multiple expression and we can write different statements for these different if expressions. In this above case, every if will be checked one by one. This will not be the ideal case to use if we want to execute only one condition on the basis of the given expression. We can use this if in a different way using the else if keyword to handle this.
5. Using else if Statements
Syntax:
if (expression 1){
statement 1
statement 2
}
else if (expression 2){
statement 3
statement 3
}
In this case, if the first expression if true then next else-if will be skipped by the program. We can also use the else at the end of these else if expression and the statements.
6. Ternary if else Statements
Syntax:
$var = (5 > 2 ? "This will be printed in case of true": "false");
Now, this is trending for the developers if they have a single comparison and a very small line of code. We can say this comparison is an inline way of handling the condition.
How Does if Statement Work in PHP?
The working of this if and else is just a basic real-life conditional-based stuff. If we do something we will get something if not if we will get nothing. That means if any expression is true in this case the code under that condition will be executer if not then will be executed. If we saw the syntax various if-else available in the PHP language. Now it’s time to start will be example using the syntax mentioned above.
Example #1 – Single Line if Statement
Code:
<?php
$var1 = 105;
$var2 = 25;
if($var1>$var2){
echo "$var1 is greater than $var2";
}
?>
Output:
Example #2 – Multi-Line if Statement
Code:
<?php
$var1 = 105;
$var2 = 25;
if($var1>$var2){
echo "$var1 is greater than $var2";
echo "\n This is second line of code.";
}
?>
Output:
Example #3 – Using if else Together
Code:
<?php
$var1 = 10;
$var2 = 20;
if($var1>$var2){
echo "$var1 is greater than $var2";
}else{
echo "$var2 is greater than $var1";
}
?>
Output:
Example #4 – Multiple if Statements
Code:
<?php
$var1 = 10;
$var2 = 20;
if($var1>$var2){
echo "$var1 is greater than $var2";
}
if($var2>$var1){
echo "$var2 is greater than $var1";
}
?>
Output:
Example #5 – Using else if
Code:
<?php
$var1 = 10;
$var2 = 20;
$var3 = 65;
if($var1>$var2){
echo "$var1 is greater than $var2";
}
else if($var2>$var1){
echo "$var2 is greater than $var1";
}
else if($var3>$var1){
echo "$var3 is greater than $var1";
}
else if($var3>$var2){
echo "$var3 is greater than $var2";
}
?>
Output:
We can see, there are other conditions that are true but its output is giving when the first condition matched. Yes, in case of multiple if and else-if group the first true statement will be executed and other remaining will be skipped. So, the developer or the programmer should use this condition very carefully. If we will not be putting the attention in using these kinds of the statement then this could lead to a serious issue in any application.
Example #6 – Ternary if else
Code:
<?php
$var1 = 15;
$var2 = 25;
$greater_val = ($var1 > $var2 ? "$var1 is greater than $var2" : "$var2 is greater $var1"); // use of ternary if
echo $greater_val;
?>
Output:
Conclusion
We should use the if else statements whenever we need decision-making things in our programming code. We can use the Ternary if else for small and a quick if else condition-based solution. Any organization or individual should use the optimum one as per their business requirements.
Recommended Articles
This is a guide to PHP if Statement. Here we discuss the Introduction and different syntax for PHP if statement along with examples and code implementation. You may also look at the following articles to learn more –