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

<?php
if(!isset($a)):
    xxx;
endif;
阅读 13.4k
1 个回答

主要是在 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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题