iT邦幫忙

0

PHP語法問題

請問這個PHP語法是什麼意思
$module = empty($_GET['mod']) ? 'main':db_escape($_GET['mod']);

其中 ? 'main':db_escape 到底是什麼意思有高手可以指點一下嗎?

看更多先前的討論...收起先前的討論...
小成 iT邦高手 10 級 ‧ 2012-07-05 13:41:00 檢舉
google 三元運算子
maxcp iT邦新手 4 級 ‧ 2012-07-05 14:03:12 檢舉
奇怪if ... else怎麼叫三元運算子?怪怪~這名詞第一次聽過!感謝指點!
krarm iT邦好手 1 級 ‧ 2012-07-05 14:54:29 檢舉
想一下一元運算子 ==> !
小成 iT邦高手 10 級 ‧ 2012-07-05 15:20:45 檢舉
silly iT邦好手 2 級 ‧ 2012-07-05 17:56:36 檢舉
紙鶴呢
maxcp iT邦新手 4 級 ‧ 2012-07-05 18:28:22 檢舉
這個一元來頭不小還有兩個影子~!
總裁 iT邦好手 1 級 ‧ 2012-07-05 20:25:04 檢舉
silly提到:
紙鶴呢

這表示了一件事....那個一元可能不是我的...冷
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

18
ihon822
iT邦研究生 2 級 ‧ 2012-07-05 13:16:51
最佳解答

(判斷條件)?(判斷條件為真時的返回值):(判斷條件不為真時的返回值)
基本上就是 if ... else ... 的簡化版

maxcp iT邦新手 4 級 ‧ 2012-07-05 13:44:25 檢舉

抱歉~發問後六小時才可以選為最要解^^

maxcp iT邦新手 4 級 ‧ 2012-07-05 13:47:31 檢舉

感謝您了~我懂了!

8
wiseguy
iT邦超人 1 級 ‧ 2012-07-05 15:28:51

這是 PHP 從 C 語言學來的簡化 if ... else ... 語法。如 ihon822 所言。
PHP 算是 C 語言系列的,所以有很多簡化語法,不過使用上以看得懂為原則。

<pre class="c" name="code">$module = empty($_GET['mod']) ? 'main' : db_escape($_GET['mod']);
$module = $_GET['mod'] ? db_escape($_GET['mod']) : 'main';
($module = db_escape($_GET['mod'])) OR ($module = 'main');

以上三種 PHP 寫法的效果都一樣。

maxcp iT邦新手 4 級 ‧ 2012-07-05 15:56:42 檢舉

哈~看到($module = db_escape($_GET['mod'])) OR ($module = 'main'); 親切多了~^^

我要發表回答

立即登入回答