wordpress中的Php条件

时间:2012-08-22 09:46:01

标签: php wordpress conditional

有人可以向我解释一下在查看word press中的php用法时经常遇到的替代语法的使用。以条件为例:

我希望看到:

if(10==10)
echo 'this is true and will be shown';

相反,我明白了:

if(10==10):
echo 'this is true and will be shown;
end if;

另一个例子:

! empty ( $classes_names ) 
and  $class_names = ' class="'. esc_attr( $class_names ) . '"';

什么是':''结束if;'最后一个示例语法不是我之前看到的那样的东西。

1 个答案:

答案 0 :(得分:6)

这是PHP中的替代逻辑语法 - 您可以在此处阅读所有相关内容 - http://php.net/manual/en/control-structures.alternative-syntax.php

如果您在HTML或其他代码中包含条件语句,那么它会使事情看起来更整洁。

例如:

<body>
<? if($_GET['name']=='Dave') {?>
<h3>Hello Dave!</h3>
<? } else { ?>
<h3>Hello Stranger!</h3>
<? }?>
</body>

看起来更好的IMO:

<body>
<? if($_GET['name']=='Dave'):?>
<h3>Hello Dave!</h3>
<? else:?>
<h3>Hello Stranger!</h3>
<? endif;?>
</body>

至于最终的代码示例 - 这是编写if语句的另一种方式 - 所以这个:

<? !empty ( $classes_names ) and  $class_names = ' class="'. esc_attr( $class_names ) . '"';?>

与:

相同
<? 
if (!empty($classes_names)){
$class_names = ' class="'. esc_attr( $class_names ) . '"';
}
?>

肯定会让人感到困惑!