我认为在视图脚本中同时使用if
s,for
s和foreach
es 时,这尤其清晰:
<?php if ( $this->hasIterable ): ?>
<h2>Iterable</h2>
<ul>
<?php foreach ( $this->iterable as $key => $val ):?>
<?php for ( $i = 0; $i <= $val; $i++ ): ?>
<li><?php echo $key ?></li>
<?php endfor; ?>
<?php endforeach; ?>
</ul>
<?php elseif ( $this->hasScalar ): ?>
<h2>Scalar</h2>
<?php for ( $i = 0; $i <= $this->scalar; $i++ ): ?>
<p>Foo = Bar</p>
<?php endfor; ?>
<?php else: ?>
<h2>Other</h2>
<?php if ( $this->otherVal === true ): ?>
<p>Spam</p>
<?php else: ?>
<p>Eggs</p>
<?php endif; ?>
<?php endif; ?>
相对于:
<?php if ( $this->hasIterable ){ ?>
<h2>Iterable</h2>
<ul>
<?php foreach ( $this->iterable as $key => $val ){?>
<?php for ( $i = 0; $i <= $val; $i++ ){ ?>
<li><?php echo $key ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } elseif ( $this->hasScalar ){ ?>
<h2>Scalar</h2>
<?php for ( $i = 0; $i <= $this->scalar; $i++ ){ ?>
<p>Foo = Bar</p>
<?php } ?>
<?php } else { ?>
<h2>Other</h2>
<?php if ( $this->otherVal === true ){ ?>
<p>Spam</p>
<?php } else { ?>
<p>Eggs</p>
<?php } ?>
<?php } ?>
这对于长控制语句特别有用,在这种情况下,您可能无法从底部括号看到顶部的声明。