2014-07-27 32 views
-4

我目前正在努力改進PHP時間表。時間表正在使用的聲明是:PHP時間表語句

echo $array2['username'] ? $array2['username'] : '-'; 

我知道echo會顯示信息,但爲什麼會有問號?冒號的目的是什麼?

我想優化上面的代碼以包含PHP if語句和一個href標籤,所以我需要知道上面的語句是如何分解以便我可以適當地重新編碼。

+1

尋找PHP三元語句。 –

+1

[參考 - 這個符號在PHP中意味着什麼?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Clive

+0

它的一個三元語句閱讀這個http://davidwalsh.name/php-shorthand-if-else-ternary-operators –

回答

1

那些是ternary operators。它相當於一個if-else聲明。

以上聲明嘗試查看索引username是否存在或設置。如果true,它使用索引username的值。否則,它返回一個空字符串。

參考:http://en.wikipedia.org/wiki/%3F