Home >Backend Development >PHP Tutorial >php 里的 if(): endif; 类似写法具体有何好处?

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

WBOY
WBOYOriginal
2016-06-06 20:44:102025browse

<code class="lang-php"><?php if(!isset($a)):
    xxx;
endif;
</code></code>

回复内容:

<code class="lang-php"><?php if(!isset($a)):
    xxx;
endif;
</code></code>

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

<code class="lang-php"> if(isset($a)): ?>
    if($a): ?>
       <span>variable a is exist and not empty</span>
    endif; ?>
 endif; ?>
</code>

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

<code class="lang-php"> if(isset($a)) { ?>
    if($a) { ?>
       <span>variable a is exist and not empty</span>
    } ?>
 } ?>
</code>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn