首页 > php 里的 if(): endif; 类似写法具体有何好处?

php 里的 if(): endif; 类似写法具体有何好处?

<?php
if(!isset($a)):
    xxx;
endif;

主要是在 HTML 模板里用这种写法比较清晰,尤其当配合 short_open_tag 时。

<? if(isset($a)): ?>
   <? if($a): ?>
       <span>variable a is exist and not empty</span>
   <? endif; ?>
<? endif; ?>

作为对比,要比下面的写法好看一点,尤其是当逻辑复杂的时候,很可能分不清花括号。

<? if(isset($a)) { ?>
   <? if($a) { ?>
       <span>variable a is exist and not empty</span>
   <? } ?>
<? } ?>

http://php.net/manual/zh/control-structures.alternative-syntax.php
http://php.net/manual/zh/ini.core.php#ini.short-open-tag

【热门文章】
【热门文章】