超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<elseifwhile>
Last updated: Fri, 22 Jun 2007

流程控制的替代语法

PHP 提供了一些流程控制的替代语法,包括 ifwhileforforeachswitch。替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;endwhile;endfor;endforeach; 以及 endswitch;

<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>

在上面的例子中,HTML 内容“A is equal to 5”用替代语法嵌套在 if 语句中。该 HTML 的内容仅在 $a 等与 5 时显示。

替代语法同样可以用在 elseelseif 中。下面是一个包括 elseifelseif 结构用替代语法格式写的例子:

<?php
if ($a == 5):
   print
"a equals 5";
   print
"...";
elseif (
$a == 6):
   print
"a equals 6";
   print
"!!!";
else:
   print
"a is neither 5 nor 6";
endif;
?>

更多例子参见 whileforif




add a note add a note User Contributed Notes
流程控制的替代语法
justin at iqmail dot net
30-Dec-2000 07:03
This is also relevant when mixing styles and interspersing PHP with HTML. The following example gives a syntax error since it associates the else with the inner if.

<?php
if (condition):
 // code
if (another_condition) {
    // multi-line code
}
else:
?>
[html here]
<?php
endif;
?>

A way around this is to not mix syntax, and change the inner if to the alternate colon-based syntax.

-jcl-

<elseifwhile>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22