How can I understand nested ?: operators in PHP? [

2019-02-26 09:38发布

Possible Duplicate:
Problem with PHP ternary operator

I was reading up a bit on PHP in this article, and I stopped for a while to consider one of his gripes. I can't figure out how on earth PHP comes to the result that it does.

Unlike (literally!) every other language with a similar operator, ?: is left associative. So this:

$arg = 'T';   
$vehicle = ( ( $arg == 'B' ) ? 'bus' :
            ( $arg == 'A' ) ? 'airplane' :
            ( $arg == 'T' ) ? 'train' :
            ( $arg == 'C' ) ? 'car' :
            ( $arg == 'H' ) ? 'horse' :
            'feet' );   
echo $vehicle;

prints horse.

What logical path does PHP follow that results in 'horse' being assigned to $vehicle?

2条回答
家丑人穷心不美
2楼-- · 2019-02-26 10:10

Note:

It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:

<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');

// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right

// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');

// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>

http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

查看更多
来,给爷笑一个
3楼-- · 2019-02-26 10:23

Bracketing is the solution for both understanding and fixing:

This should have the unintended result (horse):

$arg = 'T';  
$vehicle = (
    (
        (
            (
                (
                    ( $arg == 'B' ) ? 'bus' : ( $arg == 'A' )
                ) ? 'airplane' : ( $arg == 'T' )
            ) ? 'train' : ( $arg == 'C' )
        ) ? 'car' : ( $arg == 'H' )
    ) ? 'horse' : 'feet'
);  
echo $vehicle;

This should have the indended result (train):

$arg = 'T';   
$vehicle = (
    ( $arg == 'B' ) ? 'bus' : (
        ( $arg == 'A' ) ? 'airplane' : (
            ( $arg == 'T' ) ? 'train' : (
                ( $arg == 'C' ) ? 'car' : (
                    ( $arg == 'H' ) ? 'horse' : 'feet'
                )
            )
        )
    )
);   
echo $vehicle;
查看更多
登录 后发表回答