?? control-structures.alternative-syntax.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Alternative syntax for control structures</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.elseif.html">elseif/else if</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.while.html">while</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="control-structures.alternative-syntax" class="sect1"> <h2 class="title">Alternative syntax for control structures</h2> <p class="para"> PHP offers an alternative syntax for some of its control structures; namely, <i>if</i>, <i>while</i>, <i>for</i>, <i>foreach</i>, and <i>switch</i>. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to <i>endif;</i>, <i>endwhile;</i>, <i>endfor;</i>, <i>endforeach;</i>, or <i>endswitch;</i>, respectively. <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php </span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">5</span><span style="color: #007700">): </span><span style="color: #0000BB">?><br /></span>A is equal to 5<br /><span style="color: #0000BB"><?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> In the above example, the HTML block "A is equal to 5" is nested within an <i>if</i> statement written in the alternative syntax. The HTML block would be displayed only if <var class="varname">$a</var> is equal to 5. </p> <p class="para"> The alternative syntax applies to <i>else</i> and <i>elseif</i> as well. The following is an <i>if</i> structure with <i>elseif</i> and <i>else</i> in the alternative format: <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">5</span><span style="color: #007700">):<br /> echo </span><span style="color: #DD0000">"a equals 5"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"..."</span><span style="color: #007700">;<br />elseif (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">6</span><span style="color: #007700">):<br /> echo </span><span style="color: #DD0000">"a equals 6"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"!!!"</span><span style="color: #007700">;<br />else:<br /> echo </span><span style="color: #DD0000">"a is neither 5 nor 6"</span><span style="color: #007700">;<br />endif;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> See also <a href="control-structures.while.html" class="link">while</a>, <a href="control-structures.for.html" class="link">for</a>, and <a href="control-structures.if.html" class="link">if</a> for further examples. </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.elseif.html">elseif/else if</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.while.html">while</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -