PHP is a popular programming language that has numerous features. One such feature is shorthand if/else, which allows for more concise and readable code, this article will discuss how to use shorthand if/else in PHP.
Using Shorthand if/else in PHP
Shorthand, if/else also known as the PHP ternary operator allows you to write an if/else statement on a single line, which can make your code easier to read and understand, here, is an example:
// Longhand if/else statement
$x = 5;
$y = 3;
if ($x > $y) {
echo "X is greater than Y";
} else {
echo "Y is greater than X";
}
echo "<br>"; // Add a line break
// Shorthand if/else statement
$x = 5;
$y = 3;
echo $x > $y ? "X is greater than Y" : "Y is greater than X";
?>
We have first declared two variables $x and $y and assigned them values. We then used the longhand if/else statement to compare the values of $x and $y and output a message based on the comparison. After that, we used the shorthand if/else statement to do the same comparison and output the message on a single line.
As in the previously given example the shorthand if/else statement is written on a single line making it easier to read and understand, the syntax for shorthand if/else statements in PHP is as follows:
Here’s a breakdown of each part of the syntax:
- (condition) is the condition to check, it can be any valid expression that evaluates to either true or false.
- If the condition is true, the true statement ought to be executed.
- If the condition is false, the statement that should be executed is the false statement.
So essentially, the shorthand if/else statement evaluates the condition first so if the condition is true, it executes the true statement, otherwise, it executes the false statement. Let’s take another example where we will use shorthand if/else in a function:
function checkAge($age) {
$status = ($age < 18) ? "Child" : "Adult";
return $status;
}
echo checkAge(20); // Output: Adult
echo "<br>";
echo checkAge(15); // Output: Child
?>
In the code above, we have defined a function called checkAge that takes one parameter, $age.To determine whether the $age is under 18 inside the code, we utilize the abbreviated if/else expression. If it is, the $status variable is set to "Child", otherwise it's set to "Adult".
We then return the $status variable from the function, and finally, we call the checkAge function twice, passing in two different ages as arguments. The function returns the appropriate status for each age, and we use echo statements to output the results to the screen:
Using Shorthand if/else with Multiple Conditions in PHP
Here is another example that demonstrates the use of shorthand if/else by validating multiple conditions simultaneously and below is the code for it:
$number = 7;
$result = ($number = 5 && $number < 10) ? "Between 5 and 10" : "Greater than or equal to 10");
echo $result;
?>
In this example, we're checking if the $number variable is less than 5, between 5 and 10, or greater than or equal to 10. Depending on the value of the $number, we'll set the $result variable to one of three strings: "Less than 5", "Between 5 and 10", or "Greater than or equal to 10".
Conclusion
The shorthand if/else or ternary operator is a concise way to write if/else statements in PHP, as it allows you to write an if/else statement on a single line, making your code easier to read and understand. Shorthand if/else is a great way to simplify your code and make it more efficient, so this article discussed how to use shorthand if/else in PHP with the help of examples.