Correct Ternary Condition for IF ElseIf Condition
Here is my If Else Statement
if(isset($row['content']) && strlen($row['content'])) {
$content = $row['content'];
}
elseif(is开发者_开发百科set($row['description']) && strlen($row['description'])) {
$content = $row['description'];
}
I tried to create a condition using ternerary operator and ended for with a error: Here is my ternerary condition
$content = isset($row['content']) && strlen($row['content']) ? $row['content'] : isset($row['description']) && strlen($row['description']) ? $row['description'] : '';
What is the correct statement?
You're making your code very very unreadable by changing your condition into a ternary operator. Anyhoo, the following works without an error.
$content = (isset($row['content']) && strlen($row['content']))
? $row['content']
: (isset($row['description']) && strlen($row['description'])
? $row['description']
: '');
Wrapped the last expression in parenthesis so PHP doesn't try to evaluate it separately.
Try putting inside bracket the first term of ?:
and the last term of first ?:
.
$content = (isset($row['content']) && strlen($row['content'])) ? $row['content'] : ((isset($row['description']) && strlen($row['description'])) ? $row['description'] : '');
精彩评论