Conditional statements are famous in PHP programming, even in other programming languages because these statements support decision-making arguments and scenarios.
However, using these conditional statements consumes a lot of programming lines.
Its methods also require proper declarations of functions and variables.
As a result, we can use PHP shorthands or ternary operators as alternatives for if elseif else conditional statements.
Therefore, to understand the PHP shorthands for if/elseif/else statements, these are the ternary operators that help users shorten their conditional arguments.
To further discuss the concept of PHP shorthand if elseif else, let us have the examples below:
Use if-else Shorthand in PHP
In this section, we will have an example program where we can apply the shorthand (ternary operator) instead of the if {} else {}
statements.
Syntax:
condition ? trueExpression : falseExpression
So the purpose of this syntax is to create an alternative procedure or shorthand to the PHP if else statement that provides the same method.
To test this PHP shorthand method, we will apply it to an example program below and see if it will work as the if else statement does.
Example Program:
<?php
$sample = rand(0,20);
echo "Random number is: " . "$sample"."<br>";
echo ($sample>10)? "king":"queen";
?>
The example program tries to generate a number randomly, but with a condition.
The condition is that if the random number is greater than 10
, its corresponding string is “king
“.
But if the operator produces a number less than 10
, its corresponding string is “queen
“.
Output:
Random number is: 2 queen
The output of the program displays that the “Random number is: 2
“.
Then, since the value of the number is less than 10
, its corresponding string value is “queen
“.
However, in this example, you have to take note that the output may vary or change as you rerun the program.
Additionally, as soon as the number changes, its string value may also change according to the “($sample>10)
?” condition.
PHP Shorthand if elseif else
Next in line, we will have an example program, using the PHP shorthand for if elseif else conditional statement.
Let’s take a look at the syntax that we can apply instead of the if elseif else condition.
Syntax:
$condition ? $value_true : $value_false
Example Program:
<?php
$a = "date";
$b = "time";
$c = "c";
$d = "d";
$e = "e";
$f = "f";
$g = "g";
echo $a ? $b : ( $c ? $d : ( $e ? $f : $g ) );
?>
In this example program, it implements the ternary operator or the PHP shorthand instead of the if elseif else condition.
This means the ternary operator (?:
) is repeatedly used within the parenthesis to express the if elseif else condition.
Output:
time
PHP Shorthand if else Using Ternary Operators
Now, let us have a PHP shorthand for the if else condition example program using the ternary operators.
Example Program:
<?php
$score = 9;
$age = 9;
echo 'Your score is: '.($score > 10 ? ($age > 10 ? 'Average' : 'Exceptional') : ($age > 10 ? 'Horrible' : 'Average') );
?>
The example program takes two arguments which are the $score
and the $age
.
Then the condition also has a possible output depending on the value of the arguments.
The condition will return “Average
” if the $score
and the $age
are less than 10
. It will output “Exceptional
” if the
$score
is greater than 10
and the $age
is equal to or less than 10
.
Additionally, the program will output “Horrible
” if the $score
is less than 10
and the $age
is greater than 10
.
Output:
Your score is: Average
So since both $score
and $age
have the same value (9
) which is less than 10
, the output displays ‘Your score is: Average
‘.
PHP if else Conditional Statement Syntax and Shorthand Code Example
This time, let us have an example program where we can differentiate the use of if-else conditional statements and PHP shorthand code examples.
Example Program:
<?php
$age = 15;
if($age>18)
{
echo "Your age is $age. You are now eligible to vote.";
}
else
{
echo "Your are still minor because your age is $age.";
}
?>
Now, this example shows you the use of the if-else conditional statement in a program.
The program provides a decision-making scenario where it produces an output that specifies the possible decision according to the $age
value input.
Output:
You are still minor because your age is 15.
So since we have 15
as the value of the variable $age
, and the condition states that ($age>18)
, the output displays "You are still minor because your age is 15."
.
Related Articles
- Types of Operators In PHP (With Examples)
- PHP Convert to String with Example
- PHP Empty Function With Detailed Explanation
- PHP Upload File with Example
- GET and POST Method In PHP (GET vs POST)
Conclusion
In conclusion, the PHP shortand if elseif else topic provides the necessary information you need to completely understand the method.
The discussion above also states that the shorthand for if eleseif else is an alternative for the PHP conditional statements.
Therefore, you can shorten your PHP conditional statement through the shorthand or ternary operators that you have learned.