Strange operators in PHP


If you read the PHP documentation, you will learn about the mass of operators . If you do not know more PHP-operators, first study them, and then return to reading this article.


Judging by the first comments, I want to apologize for the bad translation, I have little experience in this, but I wanted to translate the article. I will welcome any criticism regarding the quality of the translation, especially for any improvements. Thanks for attention!

Operators usually consist of strange characters such as!, -, =>, <=>, ^ or ~. Indeed, some of them are simply readable, for example AND , while some of them are a missed attempt to make them readable and hide their virtually double personality, for example, xor .


You probably think you know the PHP documentation up and down, but there is always something to learn. So I dived deep into the PHP core code and looked at some special PHP statements, less well known, but very useful in their daily work.


So, here are 10 PHP statements that you should know in 2018!


Operator b '


Just add b before any line, and nothing happens. This clever trick only works with b and B . Here we must recall the fate of PHP 6, since b is similar to 6 .


 $string = b'content'; 

Operator [] =


A short statement for adding a new element to an array. It works as you expect - it adds an element to the right in the array on the left.


 $array []= 'element'; 

Indeed, it is much more elegant than $array[] = 'element'; . Some tests show that it is much faster, while others, on the contrary, that it is much slower. As usual, see for yourself whether a given operator is right for you before betting on performance with a similar trick.


Unfortunately, in the official manual there is not a word about this remarkable opportunity.


Operator <-


The "left object" operator was introduced in PHP to enhance the ability to write code so that developers can write PHP code from right to left. Although the project was later discontinued for unknown reasons, this first statement, which received support, was preserved in PHP.


 $c = $a<-B; 

For backward compatibility, the above code does not get the value of property B in object $a , but simply compares $a with the opposite of B


Operator ––>


--> also known as the "super-object operator", created based on its long-cousin, -> .


 $object-->property 

--> works like -> , except that it is not. The main trick: use not an object as a variable $object , but an integer (in this case it will work as a comparison > - approx. Lane)


Arrow left operator for arrays [$ a <= $ b]


We all know the operator => for arrays, but they also support the mirror version of this operator as follows:


 $array = [ 'a' => 'b', 'c' <= 'd', ]; var_dump($array); /* array(2) { ["a"]=> string(1) "b" [0]=> bool(true) */ 

This is very convenient for very young programmers, as well as those who are older, still using mirror writing. This operator also has the ability to turn both operands into a logical value, for more compact storage.


Names of constants with *,%


Constants are good and effective until you want more freedom in their syntax. Although the manual states that the name of the constant should contain only letters, underscores and numbers, but it is also allowed to use special characters such as * or% in the name. For example, as follows:


 //        ,       — . . define('A', 2); define('B', 1); define('A*B', 2); $x = foo( A*B ); 

The only thing you need to make sure that the constants A and B exist, and as a result of their multiplication, the value of the assumed constant is obtained. This is another good reason to avoid using primes as values ​​in constants.


Ship Operators


So, everyone knows about the operator "spacecraft" <=> since the release of PHP 7. But it is less known that at the same time with this release a whole fleet of spacecraft was introduced for large-scale operations.


Tiny spaceship


You can provide an escort to a spacecraft operator using a tiny spacecraft operator. This operator does not make a comparison: it simply adds $b to $a in such a refined style.


 $a -=- $b; 

X-fighters


If you want to add firepower to the previous fleet, you can call X-type fighters to the PHP source: +-0-+ . The following code subtracts 3 from $a (in the original article it is written that the operator will add 3 ( adds 3 to $a ), but this is not so, see the output of the example below - note. Trans.)


 $a = 8; $a = $a +-0-+ 3; // 5 

Battleship


The operator of the battleship provides support for previous operators. This is one of the operators that works only with arrays, just like => . It should only be used by the most advanced PHP gurus on this side of the galaxy. Do not underestimate his power.


 $x = [ 2 <=['-']=> $b]; 

Isn't that the best way to write this?


 $x = [ 1 => $b]; 

The Death Star


Many of you would be asked if the death star was built into PHP, and the answer is: unknown at this time. There are plans for this, but no one has ever seen a real copy. I would really like to see the operator in three lines, since PHP will be the first to have such a thing (and, when this happens, I hope, they will not write off the cost of this operator from my account).


  $x = $y ~~ ( °) ~~ true; 

PHP operator madness


PHP operators are neat and efficient. We hope that these lesser-known operators have taught you something in PHP.


Check out the following code examples: they all work in PHP 7.2.5, except for the death star. They may have some prerequisites, so be sure to read the documentation before using them.

Source: https://habr.com/ru/post/412603/


All Articles