开发者

What is meant by this php statement

Hello I have read f开发者_运维技巧ollowing php statement from a blog but I am unable to understand its meaning. Is it treated as if condition or any thing else? Statement is

<?= ($name== 'abc' || $name== 'def' || $name== 'press') ? 'inner-pagehead' : ''; ?>


You can read this as:

if($name=='abc' || $name=='def' || $name=='press') {
  echo 'inner-pagehead';
} else {
  echo '';
}

The <?= is the echo() shortcut syntax, then the (test)?true:false; is a ternary operation


It is saying that if $name is any one of those 3 values ("abc","def", or "press"), then display the text "inner-pagehead". Otherwise, don't display anything.


This is what I would call a poorly written Ternary condition. it basically echos 'inner-pagehead' if the $name variable matches any of the three conditions. I would have done it like this:

<?php
    echo in_array($name, array('abc', 'def', 'press')) ? 'inner-pagehead' : '';
?>

Or, even better:

// somewhere not in the view template
$content = in_array($name, array('abc', 'def', 'press')) ? 'inner-pagehead' : '';

// later, in the view template
<?php echo $content; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜