1

This is not exactly a "problem", but more a "why" question.

Based on the following example:

echo 'test' . ( true ?  : 'some-test' );

Why is the result of this: test1 instead of what one might expect: test.

Or in other words: Why is an empty return statement 1 (or actually true) instead of null ?

4

4 に答える 4

8

PHP 5.3以降では、三項演算子の中間部分を?:省略できます。
foo ?: barと同等foo ? foo : barです。したがってtrue ?: ...、常に最初の を返しますtrue

foo ? : bar「true の場合は何も返さない」という意味で、常に無効でした。この式何かを返す必要があるため、単に何も返さないわけではありません。どちらかといえば、これが欲しいでしょう: foo ? null : bar.

于 2014-04-28T13:09:10.253 に答える
4

それはPHP 5.3のせいです

expr1 ?: expr3「 PHP 5.3 以降、三項演算子の中間部分を省略することが可能にexpr1なりexpr1ましTRUEexpr3

三項演算子

于 2014-04-28T13:09:11.923 に答える
0

var_dump(true ? : 'some-test');bool(true)

var_dump('test' . true);string(5) "test1"

この部分は明らかです。ここでの stage は、 とtrue ? : 'some-test'評価されることtrueです。これは PHP 5.3 で導入された新しい動作で、中央の式を省略すると最初の式 (このtrue場合) の値が返されます。

于 2014-04-28T13:10:16.673 に答える