Troubleshooting “Unexpected T_ECHO” in ternary operator statement

后端 未结 4 1948
误落风尘
误落风尘 2020-12-03 10:08
($DAO->get_num_rows() == 1) ? echo(\"is\") : echo(\"are\");

This dose not seem to be working for me as intended, I get an error \"Unexpected T_E

4条回答
  •  眼角桃花
    2020-12-03 10:58

    The ternary operator should result in a value -- and not echo it.


    Here, you probably want this :

    echo ($DAO->get_num_rows() == 1) ? "is" : "are";
    


    If you want to use two echo, you'll have to work with an if/else block :

    if ($DAO->get_num_rows() == 1) {
        echo "is";
    } else {
        echo "are"
    }
    

    Which will do the same thing as the first portion of code using the ternary operator -- except it's a bit longer.

提交回复
热议问题