control-structures.for.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 130 行
HTML
130 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>for</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.do.while.html">do-while</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.foreach.html">foreach</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.for" class="sect1"> <h2 class="title"><i>for</i></h2> <p class="para"> <i>for</i> loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a <i>for</i> loop is: <div class="informalexample"> <div class="example-contents"><div class="cdata"><pre>for (expr1; expr2; expr3) statement</pre></div> </div> </div> </p> <p class="simpara"> The first expression (<var class="varname">expr1</var>) is evaluated (executed) once unconditionally at the beginning of the loop. </p> <p class="simpara"> In the beginning of each iteration, <var class="varname">expr2</var> is evaluated. If it evaluates to <b><tt>TRUE</tt></b>, the loop continues and the nested statement(s) are executed. If it evaluates to <b><tt>FALSE</tt></b>, the execution of the loop ends. </p> <p class="simpara"> At the end of each iteration, <var class="varname">expr3</var> is evaluated (executed). </p> <p class="simpara"> Each of the expressions can be empty or contain multiple expressions separated by commas. In <var class="varname">expr2</var>, all expressions separated by a comma are evaluated but the result is taken from the last part. <var class="varname">expr2</var> being empty means the loop should be run indefinitely (PHP implicitly considers it as <b><tt>TRUE</tt></b>, like C). This may not be as useless as you might think, since often you'd want to end the loop using a conditional <a href="control-structures.break.html" class="link"><i>break</i></a> statement instead of using the <i>for</i> truth expression. </p> <p class="para"> Consider the following examples. All of them display the numbers 1 through 10: <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: #FF8000">/* example 1 */<br /><br /></span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* example 2 */<br /><br /></span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; ; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">> </span><span style="color: #0000BB">10</span><span style="color: #007700">) {<br /> break;<br /> }<br /> echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* example 3 */<br /><br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />for (; ; ) {<br /> if (</span><span style="color: #0000BB">$i </span><span style="color: #007700">> </span><span style="color: #0000BB">10</span><span style="color: #007700">) {<br /> break;<br /> }<br /> echo </span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$i</span><span style="color: #007700">++;<br />}<br /><br /></span><span style="color: #FF8000">/* example 4 */<br /><br /></span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$j </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700"><= </span><span style="color: #0000BB">10</span><span style="color: #007700">; </span><span style="color: #0000BB">$j </span><span style="color: #007700">+= </span><span style="color: #0000BB">$i</span><span style="color: #007700">, print </span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #0000BB">$i</span><span style="color: #007700">++);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> Of course, the first example appears to be the nicest one (or perhaps the fourth), but you may find that being able to use empty expressions in <i>for</i> loops comes in handy in many occasions. </p> <p class="para"> PHP also supports the alternate "colon syntax" for <i>for</i> loops. <div class="informalexample"> <div class="example-contents"><div class="cdata"><pre>for (expr1; expr2; expr3): statement ...endfor;</pre></div> </div> </div> </p> <p class="simpara"> Its a common thing to many users to iterate though arrays like in the example below. </p> <p class="para"> <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: #FF8000">/*<br /> * This is an array with some data we want to modify <br /> * when running though the for loop.<br /> */<br /></span><span style="color: #0000BB">$people </span><span style="color: #007700">= Array(<br /> Array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Kalle'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=> </span><span style="color: #0000BB">856412</span><span style="color: #007700">), <br /> Array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Pierre'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=> </span><span style="color: #0000BB">215863</span><span style="color: #007700">)<br /> );<br /><br />for(</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">sizeof</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">); ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #0000BB">$people</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">][</span><span style="color: #DD0000">'salt'</span><span style="color: #007700">] = </span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">000000</span><span style="color: #007700">, </span><span style="color: #0000BB">999999</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> The problem lies in the second for expression. This code can be slow because it has to calculate the size of the array on each iteration. Since the size never change, it can be optimized easily using an intermediate variable to store the size and use in the loop instead of sizeof. The example below illustrates this: </p> <p class="para"> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$people </span><span style="color: #007700">= Array(<br /> Array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Kalle'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=> </span><span style="color: #0000BB">856412</span><span style="color: #007700">), <br /> Array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Pierre'</span><span style="color: #007700">, </span><span style="color: #DD0000">'salt' </span><span style="color: #007700">=> </span><span style="color: #0000BB">215863</span><span style="color: #007700">)<br /> );<br /><br />for(</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$size </span><span style="color: #007700">= </span><span style="color: #0000BB">sizeof</span><span style="color: #007700">(</span><span style="color: #0000BB">$people</span><span style="color: #007700">); </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">$size</span><span style="color: #007700">; ++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #0000BB">$people</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">][</span><span style="color: #DD0000">'salt'</span><span style="color: #007700">] = </span><span style="color: #0000BB">rand</span><span style="color: #007700">(</span><span style="color: #0000BB">000000</span><span style="color: #007700">, </span><span style="color: #0000BB">999999</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.do.while.html">do-while</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.foreach.html">foreach</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 + =
减小字号Ctrl + -
显示快捷键?