BIO*_*IOS 0 php wordpress conditional
有人可以向我解释一下在查看word press中的php用法时经常遇到的替代语法的使用.以条件为例:
我希望看到:
if(10==10)
echo 'this is true and will be shown';
Run Code Online (Sandbox Code Playgroud)
相反,我看到:
if(10==10):
echo 'this is true and will be shown;
end if;
Run Code Online (Sandbox Code Playgroud)
另一个例子:
! empty ( $classes_names )
and $class_names = ' class="'. esc_attr( $class_names ) . '"';
Run Code Online (Sandbox Code Playgroud)
什么是':''结束如果;' 并且最后一个示例语法不是我之前看到的那样的东西.
这是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>
Run Code Online (Sandbox Code Playgroud)
看起来更好的IMO:
<body>
<? if($_GET['name']=='Dave'):?>
<h3>Hello Dave!</h3>
<? else:?>
<h3>Hello Stranger!</h3>
<? endif;?>
</body>
Run Code Online (Sandbox Code Playgroud)
至于最终的代码示例 - 这是编写if语句的另一种方式 - 所以这个:
<? !empty ( $classes_names ) and $class_names = ' class="'. esc_attr( $class_names ) . '"';?>
Run Code Online (Sandbox Code Playgroud)
是相同的:
<?
if (!empty($classes_names)){
$class_names = ' class="'. esc_attr( $class_names ) . '"';
}
?>
Run Code Online (Sandbox Code Playgroud)
这肯定令人困惑!