PHP Shorthand ternary operator “?:” Parse error unexpected “:”

后端 未结 2 1512
日久生厌
日久生厌 2020-12-15 16:31

I\'ve just uploaded some old PHP files to a new server and am getting parse errors (Unexpected \':\') on shorthand ternary ops. eg:

$y = $x ?: \"Some default         


        
相关标签:
2条回答
  • 2020-12-15 17:00

    This is only available since PHP 5.3

    The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

    Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.1

    See this example for more context.

    or a more useful but note in the comments: http://www.php.net/manual/en/control-structures.if.php#102060


    1http://php.net/manual/en/language.operators.comparison.php

    0 讨论(0)
  • 2020-12-15 17:05

    Since you are using php 5.2.16, your ternary requires 2 options e.g

    $y = $x? "???" : "Some default";
    

    Variable = condition ? true value : false value ;

    0 讨论(0)
提交回复
热议问题