- PHP if. else. elseif Statements
- PHP Conditional Statements
- PHP — The if Statement
- Syntax
- Example
- PHP — The if. else Statement
- Syntax
- Example
- PHP — The if. elseif. else Statement
- Syntax
- Example
- PHP — The switch Statement
- PHP One Line If Statement
- PHP If Statement
- PHP If…Else
- PHP If…Elseif…Else
- PHP One Line If Statement
- Conclusion
- About the author
- John Otieno
- One Line if Statement in PHP
- if Statement in PHP
- Syntax
- Example
- if. else Statement in PHP
- Syntax
- Example
- if. elseif. else Statement in PHP
- Syntax
- Example
- Ternary Operator to Provide the One Line if Statement in PHP
- Syntax
- Example:
- One line if condition in php
PHP if. else. elseif Statements
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement — executes some code if one condition is true
- if. else statement — executes some code if a condition is true and another code if that condition is false
- if. elseif. else statement — executes different codes for more than two conditions
- switch statement — selects one of many blocks of code to be executed
PHP — The if Statement
The if statement executes some code if one condition is true.
Syntax
Example
Output «Have a good day!» if the current time (HOUR) is less than 20:
PHP — The if. else Statement
The if. else statement executes some code if a condition is true and another code if that condition is false.
Syntax
if (condition) code to be executed if condition is true;
> else code to be executed if condition is false;
>
Example
Output «Have a good day!» if the current time is less than 20, and «Have a good night!» otherwise:
if ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>
PHP — The if. elseif. else Statement
The if. elseif. else statement executes different codes for more than two conditions.
Syntax
if (condition) code to be executed if this condition is true;
> elseif (condition) code to be executed if first condition is false and this condition is true;
> else code to be executed if all conditions are false;
>
Example
Output «Have a good morning!» if the current time is less than 10, and «Have a good day!» if the current time is less than 20. Otherwise it will output «Have a good night!»:
if ($t < "10") echo "Have a good morning!";
> elseif ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>
PHP — The switch Statement
The switch statement will be explained in the next chapter.
PHP One Line If Statement
Decision-making is a critical part of any productive application. Conditional statements allow us to evaluate matching conditions and take action accordingly.
In PHP, decision-making constructs are implemented using if and if…else statements. Let us discuss using these statements and implement them in our programs.
PHP If Statement
The if statement in PHP allows you to check for a specific condition and perform a particular action if the state is true or false.
The syntax is shown below:
The program checks the condition in parenthesis. If the condition is true, it executes the code inside the curly braces.
We can illustrate this with an example as shown below:
The previous code checks if the value stored by the $age variable is greater than 18. If true, it prints the string “pass!!”.
The output is shown below:
PHP If…Else
In the previous example, we check for one condition and act if it is true. However, if the condition is false, the program does not do anything.
We can use an if…else block to specify the action if the condition is false.
The following syntax is provided:
The following example is shown:
In this example, we set the value of the $age variable to 10. Then, we use an if..else block to check if the age is greater than 18. If true, echo “Pass!!” else print “Denied!!”.
The previous code should return output as shown below:
PHP If…Elseif…Else
The other condition constructs in PHP is the if..elseif..else statement. This allows you to evaluate multiple conditions in a single block.
The following syntax is shown:
if ( test condition 1 ) {
// action if condition 1 is true
} elseif ( test condition 2 ) {
// action if condition 2 is true
} else {
// action if all are false
}
We can implement the following example:
If we run the previous code, we should get the following output:
PHP One Line If Statement
PHP provides us with a ternary operator to create a one-line if statement. It acts as a more concise version of an if…else statement.
The syntax is provided below:
Here’s the following example :
Both the ternary operator and an if…else statements work similarly. However, one is more verbose and readable, while the other is minimal and concise.
Conclusion
This tutorial covered conditional statements in PHP, including the ternary operator statement.
In addition, examples were provided to evaluate the matching conditions. Check other Linux Hint articles for more tips and tutorials.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list
One Line if Statement in PHP
- if Statement in PHP
- if. else Statement in PHP
- if. elseif. else Statement in PHP
- Ternary Operator to Provide the One Line if Statement in PHP
We as programmers often have to make decisions based on certain conditions and write code that is executed by the program if the conditions are met. The if statement is a decision-making statement available in all programming languages. We will learn about one line if statement & its alternatives in PHP.
PHP supports 4 differing types of Conditional Statements. All the conditional statements support logical operators inside the condition, such as && and || .
if Statement in PHP
The if statement will decide the flow of execution. It executes the code of the if block only when the condition matches. The program evaluates the code sequentially; if the first condition is true, all other conditions in the sequence will be ignored. This is true for all the conditional statements.
Syntax
if(condition) // Code to be executed >
Example
php $grade = "A"; if($grade = "A") echo "Passed with Distinction"; > ?>
if. else Statement in PHP
It executes the code of the if block if the condition matches; otherwise, it executes the code of the else block. An alternative choice of an else statement to the if statement enhances the decision-making process.
Syntax
if(condition) // Code to be executed if condition is matched and true > else // Code to be executed if condition does not match and false >
Example
php $mark = 30; if($mark >= 35) echo "Passed"; > else echo "Failed"; > ?>
if. elseif. else Statement in PHP
It Executes the code based on the matching condition. If no condition matches, the default code will be executed written inside the else block. It combines many if. else statements. The program will try to find out the first matching condition, and as soon as it finds outs matching condition, it executes the code inside it and breaks the if loop. If no else statement is given, the program will execute no code by default, and the code following the last elseif will be executed.
Syntax
if (test condition 1) // Code to be executed if test condition 1 is true > elseif (test condition 2) // Code to be executed if the test condition 2 is true and condition1 is false > else // Code to be executed if both conditions are false >
Example
php $mark = 45; if($mark >= 75) echo "Passed with Distinction"; > else if ($mark > 35 && $mark 75) echo "Passed with first class"; > else echo "Failed"; > ?>
Ternary Operator to Provide the One Line if Statement in PHP
It is an alternative to if. else because it provides an abbreviated way of writing the if. else statements. Sometimes it becomes difficult to read the code written using the ternary operator. Yet, developers use it because it provides a great way to write compact if-else statements.
Syntax
(Condition) ? trueStatement : falseStatement
- Condition ? : A condition to check
- trueStatement : A result if condition matches
- falseStatement : A result if the condition does not match
The ternary operator selects the value on the left of the colon if the condition evaluates to be true and selects the value on the right of the colon if the condition evaluates to be false.
Let’s check the following examples to understand how this operator works:
Example:
php $mark = 38; if($mark > 35) echo 'Passed'; // Display Passed if mark is greater than or equal to 35 > else echo 'Failed'; // Display Failed if mark is less than 35 > ?>
php $mark = 38; echo ($mark > 35) ? 'Passed' : 'Failed'; ?>
There is no difference between both these statements at the byte code level. It writes compact if-else statements, nothing else. Bear in mind that ternary operators are not allowed in some code standards because it decreases the readability of code.
Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.
One line if condition in php
elseif , as its name suggests, is a combination of if and else . Like else , it extends an if statement to execute a different statement in case the original if expression evaluates to false . However, unlike else , it will execute that alternative expression only if the elseif conditional expression evaluates to true . For example, the following code would display a is bigger than b , a equal to b or a is smaller than b :
if ( $a > $b ) echo «a is bigger than b» ;
> elseif ( $a == $b ) echo «a is equal to b» ;
> else echo «a is smaller than b» ;
>
?>?php
There may be several elseif s within the same if statement. The first elseif expression (if any) that evaluates to true would be executed. In PHP, it’s possible to write else if (in two words) and the behavior would be identical to the one of elseif (in a single word). The syntactic meaning is slightly different (the same behavior as C) but the bottom line is that both would result in exactly the same behavior.
The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to false , and the current elseif expression evaluated to true .
Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define if / elseif conditions, the use of elseif in a single word becomes necessary. PHP will fail with a parse error if else if is split into two words.
/* Incorrect Method: */
if ( $a > $b ):
echo $a . » is greater than » . $b ;
else if ( $a == $b ): // Will not compile.
echo «The above line causes a parse error.» ;
endif;
/* Correct Method: */
if ( $a > $b ):
echo $a . » is greater than » . $b ;
elseif ( $a == $b ): // Note the combination of the words.
echo $a . » equals » . $b ;
else:
echo $a . » is neither greater than or equal to » . $b ;
endif;