Home > Backend Development > PHP Tutorial > PHP if Statement

PHP if Statement

王林
Release: 2024-08-29 12:40:08
Original
894 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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
Copy after login

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
}
Copy after login

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
}
Copy after login

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
}
Copy after login

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
}
Copy after login

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");
Copy after login

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";
}
?>
Copy after login

Output:

PHP if Statement

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.";
}
?>
Copy after login

Output:

PHP if Statement

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";
}
?>
Copy after login

Output:

PHP if Statement

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";
}
?>
Copy after login

Output:

PHP if Statement

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";
}
?>
Copy after login

Output:

PHP if Statement

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;
?>
Copy after login

Output:

PHP if Statement

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.

The above is the detailed content of PHP if Statement. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template