Weird operators in PHP
If you read the PHP documentation, you will learn about a ton of operators. If you haven’t learnt about PHP operators, go do that first, we’ll wait for you.
Operators are usually made up with strange symbols, like !, -, =>, <=>, ^ or ~. Really, some are plain readable like
and, while some are merely an missed attempt at being readable, and actually hide a double personnality, like xor.
You probably think you know PHP’s documentation in and out, but there is always more to learn. So I dove deep into the core of PHP code, and looked some special PHP operators that are lesser known, but very useful in your daily coding.
Here are 10 PHP operators you have to know in 2018!
Simply add a b before any string, and it will do nothing. This neat trick works with b and B and nothing else. It is here to remind us of the fate of PHP 6, since
b looks like a
$string = b'content';
The short array append operator. It works just as you expect, by pushing the element on the right side to the array on the left.
$array = 'element';
Indeed, it is a lot more elegant than
$array = 'element';. Some benchmarks tends to show it is a lot faster while other benchmarks show it is a lot slower. As usual, check if it applies to you before betting all your performances on such a trick.
Sadly, there is not a word about this great tool in the manual.
On the left object operator <-
The left object operator was introduced in PHP to extend the ways of writing code : coders could write PHP right to left. Although the project was later discontinued for obscure reasons, the first operator to be supported was left in PHP.
$c = $a>-B;
For backward compatibility purposes, this code doesn’t reach the property B in the object $a, but simply compares $a to the opposite of B.
– – > operator
--> is also known as the ‘super object operator’, built on top of its remote cousin,
--> works like
->, except when it doesn’t. The main catch : don’t use an object in the
$object variable, but rather an integer.
<= : left arrow for arrays [ $a <= $b ]
We all know the
=> operator for the arrays, but arrays also support the mirror version, like this :
$array = ['a' => 'b', 'c' <= 'd', ];
This is very convenient for very young coders, and those older ones that are still doing mirror-writing. This operator also has the ability to turn both operands into a boolean, for very compact storage.
Constant names with *, %
Constants are nice and efficient, until you want more freedom for their syntax. Although the manual states that constants should only have letters, underscores and figures, it is also authorized to use special characters like * or % in the name. Just like this :
$x = foo( A*B );
The only preparation you have to do is to make sure that constants A and B exist, and that their product has the value of your intended constant. This is another good reason to avoid prime numbers as constant values.
So, since PHP 7 introduced it, everyone know about the spaceship operator
<=> . It is less know that a whole fleet of spaceship was introduced at the same time, to offer large fleet scale operations.
You may provide an escort to the spaceship operator, by using the tiny spaceship operator. This operator is not a comparison : it simply adds $b to $a, with a lot of style.
$a -=- $b;
In case you want to add some firepower to the previous fleet, you can summon X-fighters to the PHP source :
+-0-+. The following code adds 3 to
$a = $a +-0-+ 3;
The battleship operator provides support for the previous operators. It is one of the ‘array only’ operators, just like =>. It should only be used by the most advanced PHP gurus ever on this side of the galaxy. Do not underestimate its power.
$x = [ 2 <=['-']=> $b];
Isn’t that a much better way to write this ?
$x = [ 1 => $b];
Many of you have asked if a ‘death star’ operator was built-in PHP, and the answer is : currently unknown. There are plans for it, but no one has ever seen any actual instance. I would love to see a three-line wide operator, as PHP would be the first to have such a thing (and when that happens, hopefully, I won’t be credited for the worth operator ever).
$x = $y ~~ ( °) ~~ true;
PHP operator madness
PHP operators are neat and efficient. We hope those lesser known operators have taught you a thing or two in PHP.
Check the provided code : they all work in PHP 7.2.5 code, except the death star. They may have some pre-requisite, so make sure to read the doc before using them.