What is a PHP Elvis Operator?
The PHP Elvis operator has the symbol “?:
” which looks like the signature look of the famous king of rock n’ roll artist, Elvis Presley.
You will see on the internet the reason why they came up with the name Elvis Operator.
However, this topic will focus more on what is the Elvis operator along with an explanation of how to use it in a program.
The Elvis operator in PHP can be utilized to eliminate redundancy in your programs and accelerate project completion.
It is equivalent to the ternary operator but without the second operand.
If the operand is valid, the first operand is returned; otherwise, the second operand is evaluated and returned.
Syntax – Elvis Operator
expression1 ?: expression2
What does Elvis’s operator do?
In PHP, the Elvis operator returns a non-null value even if the conditional expression is null.
Additionally, it is used to validate the nullability of values.
In certain situations, we can declare variables that can store null references.
This PHP Elvis operator works like the ternary or (with the ||
symbol) which means they have similar functions.
Elvis is also a binary operator that returns its first operand if the value of that first operand is true.
This operator evaluates its second operand and returns its first operand if its first operand evaluates to false.
Comparison Operators
Example | Name | Result |
$a == $b | Equal | true if $a is equal to $b after type juggling. |
$a === $b | Identical | true if $a is equal to $b, and they are of the same type. |
$a != $b | Not equal | true if $a is not equal to $b after type juggling. |
$a <> $b | Not equal | true if $a is not equal to $b after type juggling. |
$a !== $b | Not identical | true if $a is not equal to $b, or they are not of the same type. |
$a < $b | Less than | true if $a is strictly less than $b. |
$a > $b | Greater than | true if $a is strictly greater than $b. |
$a <= $b | Less than or equal to | true if $a is less than or equal to $b. |
$a >= $b | Greater than or equal to | true if $a is greater than or equal to $b. |
$a <=> $b | Spaceship | An int is less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. |
What is PHP Ternary Operator?
Ternary operators in PHP are conditional operators that reduce the amount of code required to conduct comparisons and conditions.
This approach is an alternative to the if-else and nested if-else operators.
Remember that left to right is the sequence of execution for this operator.
In reality, applying these ternary operators is the most efficient means of saving time.
In addition, ternary operators generate an e-notification when encountering a null value with their conditionals.
It is known as a ternary operator since it requires three operands: a condition, a result for true, and a result for false.
Syntax – Ternary operator
(Condition) ? (Statement1) : (Statement2);
This ternary operator syntax has three values parameters, which include the following:
- Condition – This expression needs to be evaluated and must return either a true or false value.
- Statement1 – it will be executed first if the evaluation results to true.
- Statement2 – this statement will be executed if the evaluation results to false.
Example Program Ternary Operator
Example Program:
<?php
$sample = 40;
echo ($sample >= 40) ? "Valid" : "Invalid";
?>
Output:
Valid
When do we use Ternary Operator?
Most developers use ternary operators when there’s a need to simplify if-else statements.
These alternative operators are used to give values to variables. Also, they are often used to validate forms or assign post data.
In short, PHP programmers use the ternary operator instead of longer if and else statements to decide what to do.
Remember, the use of PHP ternary is not always as flexible as the PHP conditional statements.
Moreover, most of the time, you will have to use conditional statements, especially in complex decision-making.
However, when you master these PHP ternary operators, such as the Elvis operator, you might be a fan of using them instead of conditional statements.
Have the example below to help yourself understand how ternary operators work.
Sample Program:
<!DOCTYPE html>
<html>
<body>
<form method="post" action="edit.php">
<label>Name<input type="text" name="name"></label>
<label>Email<input type="text" name="email"></label><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']))
{
$name = isset($_POST['name']) ? $_POST['name'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
}
?>
</body>
</html>
In this example, we use the PHP Elvis operator to evaluate the inputs from the example program.
Related Articles
- Types of Operators In PHP (With Examples)
- PHP Shorthand If ElseIf Else
- Session Name PHP With Code Example
- PHP Unlink Function With Example Program
- PHP Echo Array with Example
Conclusion
In conclusion, this discussion has stressed and covered all that should be included to deliver the exact definition of the PHP Elvis operator.
Moreover, the topic also highlights the concept of the Elvis operator as one of the PHP ternary operators.
As a reminder from this tutorial, you must be aware of the correct symbol for using the Elvis Operator along with the other PHP operators.