Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1242

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>while</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="control-structures.alternative-syntax.html">Alternative syntax for control structures</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="control-structures.do.while.html">do-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.while" class="sect1">
 <h2 class="title"><em>while</em></h2>
 <p class="verinfo">(PHP 4, PHP 5)</p>
 <p class="para">
  <em>while</em> loops are the simplest type of loop in
  PHP.  They behave just like their C counterparts.  The basic form
  of a <em>while</em> statement is:
  <div class="informalexample">
   <div class="example-contents">
<div class="cdata"><pre>
while (expr)
    statement
</pre></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  The meaning of a <em>while</em> statement is simple.  It
  tells PHP to execute the nested statement(s) repeatedly, as long
  as the <em>while</em> expression evaluates to
  <strong><code>TRUE</code></strong>.  The value of the expression is checked
  each time at the beginning of the loop, so even if this value
  changes during the execution of the nested statement(s), execution
  will not stop until the end of the iteration (each time PHP runs
  the statements in the loop is one iteration).  Sometimes, if the
  <em>while</em> expression evaluates to
  <strong><code>FALSE</code></strong> from the very beginning, the nested
  statement(s) won&#039;t even be run once.
 </p>
 <p class="para">
  Like with the <em>if</em> statement, you can group
  multiple statements within the same <em>while</em> loop
  by surrounding a group of statements with curly braces, or by
  using the alternate syntax:
  <div class="informalexample">
   <div class="example-contents">
<div class="cdata"><pre>
while (expr):
    statement
    ...
endwhile;
</pre></div>
   </div>

  </div>
 </p>
 <p class="para">
  The following examples are identical, and both print the numbers
  1 through 10:
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;example&nbsp;1&nbsp;*/<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />while&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;the&nbsp;printed&nbsp;value&nbsp;would&nbsp;be<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$i&nbsp;before&nbsp;the&nbsp;increment<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(post-increment)&nbsp;*/<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">/*&nbsp;example&nbsp;2&nbsp;*/<br /><br /></span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />while&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++;<br />endwhile;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="control-structures.alternative-syntax.html">Alternative syntax for control structures</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="control-structures.do.while.html">do-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>