Troubleshooting “Unexpected T_ECHO” in ternary operator statement

这一生的挚爱 提交于 2019-11-28 08:59:43

The Ternary operator is not identical to an if-then. You should have written it

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

It returns the value in the 2nd or 3rd position. It does NOT execute the statement in the 2nd or 3rd position.

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.

The ternary operator returns one of two values after evaluating the conditions. It is not supposed to be used the way you are using it.

This should work:

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

U can use

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!