开发者

What is meant by this php statement

开发者 https://www.devze.com 2023-04-11 03:37 出处:网络
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

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

精彩评论

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