Keep your code short with PHP's handy operators!

Useful php operators

This is Yoshino from Development!
In this article, I would like to introduce some useful operators that can be used to shorten PHP code.

Click here for table of contents

  • 1. ternary operator
  • 2. the Null coalescing operator
  • 3. elvis operator
  • 4. summary

 

conditional operator

First, I'd like to introduce probably the most famous ternary operator.
The ternary operator can be used by writing the conditional expression ? You can use it by writing expression1 : expression2.

The following is a concrete code description.


$a = 100;
$b = 100;
echo $a === $b ? '$a and $b are the same value' : '$a and $b are different values';

Execution result
$a and $b are the same value
If you describe the same process using if~else, the amount of code will increase.


$a = 100;
$b = 100;
if ($a === $b) {
    echo '$a and $b are the same value';
} else {
    echo '$a and $b are different values';
}

However, too much use of ternary operators reduces readability, so it is not recommended to use ternary operators such as calling ternary operators within ternary operators.

Use ternary operators within ternary operators


$rand = rand(1, 100);
echo 'Value:' . $rand;
echo ($rand % 2 === 0) ? (50 <= $rand ? 'even number greater than 50' : 'even number less than 50') : (50 <= $rand ? 'odd number over 50' : 'odd number under 50');

 

Use if~else and ternary operators


$rand = rand(1, 100);
echo 'Value:' . $rand;
if ($rand % 2 === 0) {
    echo 50 <= $rand ? 'Even number greater than 50' : 'Even number less than 50';
} else {
    echo 50 <= $rand ? 'odd number over 50' : 'odd number under 50';
}

Although the contents of the above two processes are the same, I think the code that combines if~else and the ternary operator is easier to understand the flow of the process.

null coalescing operator

The Null coalescing operator (??) was added in PHP 7.0 as a syntax sugar for !is_null($hoge) ? It was added in PHP 7.0 as a syntax sugar for $hoge : $fuga. The description is in the form $hoge ? $fuga.

The following is a concrete code description.


$userName = 'Tatsuno Taro';
echo $userName ? 'none';

$nickName = null;
echo $nickName ? 'none';

Execution Result
Taro Tatsuno
none

If $userName is non-null, it returns "$userName" of the first operand, and if null, it returns "none" of the second operand.
You can check if the value is set, and if not, you can shorten the description when you want to assign some value.

Elvis operator

The Elvis operator (? :) is a ternary operator (conditional expression ? expression1 : expression2) in which the expression1 part is omitted.
The description is in the form of $hode ? : $fuga.

The following is a concrete code description.


$a = 0;
echo $a ? : 'FALSE';

$b = 1;
echo $b ? : 'FALSE';

Execution Result
FALSE
1

The behavior is a bit similar to the Null coalescing operator, but
The Null coalescing operator (??) is equivalent to !is_null($hoge) ? $hoge : $fuga is equivalent to
The Elvis operator (? :) is equivalent to $hoge ? There are differences such as $hoge : equivalent to $fuga.

summary

In this article, I have introduced some operators.
Using the above operators will simplify your code, so go ahead and use them!
If you use the ternary operator, you can write conditional statements more concisely than the if statement, the code is also very convenient to clear.

en_USEnglish