在if语句中使用php结束标记是否正确? :<!-?php if(condition){?->}

时间:2019-04-23 08:43:37

标签: php opencart opencart2.x opencart2.3

在opencart 2.2 product.tpl文件中,我发现以下代码令我感到困惑:

<div class="row" style="padding-top:10px"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>

具体来说,我无法理解它们如何在if语句的大括号内使用?>。更重要的是,我尝试将上述代码修改如下,但随后停止工作:

<div class="row"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { 
$class = 'col-sm-6'; 
} 
elseif ($column_left || $column_right) { 
$class = 'col-sm-9'; 
} else { 
$class = 'col-sm-12'; 
?>

有什么解释吗?

4 个答案:

答案 0 :(得分:1)

php代码没有错误。

您可以随意打开和关闭php标签。在运行时,文件将被处理,并且所有php代码每次都会被解释。

所以可以这样写

<?php echo 'hello';
echo 'world';
?>

还有这个

<?php echo 'hello'; ?>
<?php echo 'world'; ?>

OpenCart之所以使用更高版本的样式的唯一原因是因为它<?php标记创建了一个列,并且它更易于阅读,因此更好。

答案 1 :(得分:0)

它清晰可见,if存在语法错误。没有elseif,就不能使用if

<?php 
if ($check) {
    // Code...
} elseif ($check_2) {
    // Code...
}
?>

答案 2 :(得分:0)

<div class="row"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { 
$class = 'col-sm-6'; 
} 
elseif ($column_left || $column_right) { 
$class = 'col-sm-9'; 
} else { 
$class = 'col-sm-12'; 
} // this is missing in your case
?>

答案 3 :(得分:0)

<div class="row"><?php echo $column_left; ?>

<?php if ($column_left && $column_right) { 
         $class = 'col-sm-6'; 
       } 
      elseif ($column_left || $column_right) { 
         $class = 'col-sm-9'; 
       } else { 
         $class = 'col-sm-12'; 
       } // you have missed this "}" for the else .
 ?>