2013-07-03 107 views
-5

我有這樣的代碼:將三元運算符分解爲if語句?

$get_options = isset($options['big_heading']) ? ($options['big_heading']) : '' ;但我很難讀這個三元運算符,所以我怎麼能分解成一個if語句?對不起,我的好評問題... 謝謝!

+1

嗯,使用三元運算符是罰款和可讀性比if/else語句更好。那麼,這是主觀的。 – Leri

+2

這裏發生了什麼垃圾,你認爲這是正確的空間問題。 :( –

+0

嗯Neeraj辛格不是每個人都知道PHP像你這樣做,我們有些人仍然在學習這個東西... –

回答

4

你的代碼等於:

if(isset($options['big_heading'])){ 
    $get_options = $options['big_heading']; 
} else { 
    $get_options = ''; 
} 

這句法總是喜歡:$foo = (condition) ? if_its_true : if_its_not_true ;

1

這是一樣的

$get_options = isset($options['big_heading']) ? ($options['big_heading']) : '' ; 

if(isset($options['big_heading'])) { 
    $get_options = $options['big_heading']; 
} else { 
    $get_options = ''; 
} 
1
if(isset($options['big_heading'])) { 
    $get_options = $options['big_heading']; 
} else { 
    $get_options = ''; 
} 
1
$get_options = ""; 

if(isset($options['big_heading'])) 
{ 
    $get_options = $options['big_heading'] 
} 
1
$get_options = '' ; 
if (isset($options['big_heading'])) { 
$get_options = $options['big_heading']; 
} 
1

在這裏你去...

if (isset($options['big_heading'])){ 
    $get_options = $options['big_heading']; 
else{ 
    $get_options = ''; 
}