📜 ⬆️ ⬇️

Features of the impact of learning multiple programming languages

Try calculating the result of the following expression in your mind:

true ? false ? 5 : 6 : true ? 7 : 8

Calculated? And now we go under habrakat

So, if you think that the result of calculating this expression should be 7, then most likely you are a PHP programmer. If you think that the result of the calculation of this expression should be 6, then most likely you are programming in one of the other languages.
The thing is that in comparison with other programming languages, PHP has the wrong associativity of the ternary operator. In PHP, the ternary operator has left associativity, whereas in other programming languages, this operator has right associativity. Accordingly, with the help of brackets you can show how this PHP expression evaluates:
')
(true ? (false ? 5 : 6) : true) ? 7 : 8

and how other programming languages ​​compute it:

true ? (false ? 5 : 6) : (true ? 7 : 8)

Worst of all, half of the PHP textbooks state (checked by Google search ) that the ternary operator supposedly has the right associativity, which is not true. The official PHP documentation states that the ternary operator has left associativity in PHP.
Of course, in real life, such a govnod should have its hands off, but the example was taken not from real practice, but was just a test task for finding a job. I must say that the example was much longer, and I naively believing that in PHP the ternary operator has, as elsewhere, the right associativity, gave the wrong answer.
And here is the “influence of learning several programming languages”? Often we, having already known several programming languages, skip the very common sections in textbooks on a new language for ourselves, believing that everything is just like in other languages. Be careful and trust only official documentation.
The ternary operator and its problem in PHP is a page in English on Wikipedia .

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


All Articles