Php ternary operator giving error

In PHP, if I have a triple type:

$my_thing = $this->myAttribute ? $this->myAttribute : "No attribute was set."; 

can this be reduced?

 $my_thing = $this->myAttribute ?: "No attribute was set." 

It seemed to me that I remember how PHP supported this in its tees, but now I get an error message.

+4
source share
1 answer

It is supported in PHP 5.3 and higher. From php.net

With PHP 5.3, you can exclude the middle part of the ternary operator. Expression expr1 ?: Expr3 returns expr1 if expr1 is TRUE and expr3 otherwise.

+7
source

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


All Articles