我怎麼能把這個if語句變成一個三元語句?三元聲明不按預期測試值
if (!empty($vendor_address_row['address2'])){
$address_obj['address2'] = $vendor_address_row['address2'];
}else{
$address_obj['address2'] = '<tr><td colspan="1">'.$address_row['address2'].'</td></tr>';
}
我有特里
$vendor_address_row['address2'] ? $address_obj['address2'] = '<tr><td colspan="1">'.$address_row['address2'].'</td></tr>' : $address_obj['address2'] = '';
和
!empty($vendor_address_row['address2']) ? $address_obj['address2'] = '<tr><td colspan="1">'.$address_row['address2'].'</td></tr>' : $address_obj['address2'] = '';
既不工作。除此之外,可能還有更簡單的方法。非常感謝你。
我可以說,你的標題(「_ternary語句沒有正確測試值」)是不正確的:那個測試不正確的不是你的聲明。你沒有使用比較,但是賦值和賦值在新值爲真時給出真實值。 – Tadeck