这个php语句是什么意思


What is meant by this php statement

你好,我从博客上阅读了以下php语句,但我无法理解其含义。它是作为一种疾病还是其他什么?语句

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

你可以这样读:

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

<?=echo()的快捷语法,那么(test)?true:false;是一个三元运算

如果$name是这三个值中的任何一个("abc","def"或"press"),则显示文本"inner-pagehead"。

这就是我所说的写得很差的三元条件。如果$name变量匹配三个条件中的任何一个,它基本上会响应'inner-pagehead'。我可以这样做:

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

或者更好:

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