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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
There are various ways of using this if statements, we will see all with syntax and bit description about it.
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.
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.
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.
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.
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.
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.
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.
Code:
<?php $var1 = 105; $var2 = 25; if($var1>$var2){ echo "$var1 is greater than $var2"; } ?>
Output:
Code:
<?php $var1 = 105; $var2 = 25; if($var1>$var2){ echo "$var1 is greater than $var2"; echo "\n This is second line of code."; } ?>
Output:
Code:
<?php $var1 = 10; $var2 = 20; if($var1>$var2){ echo "$var1 is greater than $var2"; }else{ echo "$var2 is greater than $var1"; } ?>
Output:
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:
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.
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:
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.
The above is the detailed content of PHP if Statement. For more information, please follow other related articles on the PHP Chinese website!