PHP中流程控制的替代语法

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

使用这种语法时需要注意:elseif不可以分开写为else if,否则会编译不通过

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//以下三种写法是正确的
if (1 > 2) :
	echo 'test1';
	echo 'test2';
endif;
 
if (1 > 2):
	echo 'test1';
	echo 'test2';
else:
	echo 'test3';
	echo 'test4';
endif;
 
if (1 > 2):
	echo 'test1';
	echo 'test2';
elseif (1 > 0):
	echo 'test3';
	echo 'test4';
endif;
 
//下面这种写法是错误的
if (1 > 2):
	echo 'test1';
	echo 'test2';
else if (1 > 0):
	echo 'test3';
	echo 'test4';
endif;
发表评论?

8 条评论。

  1. 嗯。很少用这种写法!

  2. 不错,学习了,谢谢友链。

  3. 正在学习中,以后多多请教哦~

  4. 这些我还真不知道 嘿嘿

  5. 技术问题,学习了。

  6. 代码块作用域的问题。呵呵
    PS:我已经换新域名了,麻烦更新一下友链

  7. 又转到这篇文章了。
    我现在一般在模板中用这种语法。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>