ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Ñ­»·

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

PHP
PHP Libxml
PHP Math
PHP Misc
PHP MySQL
PHP SimpleXML
PHP String
PHP XML
PHP Zip
PHP Mail
ÓÃPHP5µÄDirector..
PHP ×èÖ¹SQL×¢ÈëÊ..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
PHP5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
php5ÃæÏò¶ÔÏó - »..
ÓÃPHP¿ØÖÆÓû§µÄä..
PHP PDO ѧϰ±Ê¼Ç

PHP Ñ­»·


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:7071 :: ::

Looping statements in PHP are used to execute the same block of code a specified number of times.
PHPÑ­»·Óï¾äµÄ×÷ÓÃÊÇ£º¶Ôͬһ¸ö´úÂë¿éÖ´ÐÐÖ¸¶¨µÄ´ÎÊý¡£


Looping
Ñ­»·Óï¾ä

Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.
ÔÚÊéд´úÂëʱ£¬Äã¾­³£»á·¢ÏÖ£¬Ä³¸ö´úÂë¿ìÒª±»¶à´ÎÖ´ÐС£Èç¹û³öÏÖÁËÕâÖÖÇé¿ö£¬Äã¾Í¿ÉÒÔͨ¹ýÑ­»·Óï¾äÀ´ÊµÏÖËü¡£

In PHP we have the following looping statements:
PHPÖеÄÑ­»·Óï¾ä¾ßÌåÖ¸ÁîÈçÏ£º

  • while - loops through a block of code if and as long as a specified condition is true
    while£ºÕâ¸öÖ¸ÁîÓÃÓÚ£ºµ±Ö¸¶¨µÄÌõ¼þÎªÕæ[true]ʱ£¬Ö´ÐÐÕâ¸ö´úÂë¿é£»
  • do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true
    do...while£ºÕâ¸öÖ¸ÁîÓÃÓÚ£ºÏÈÖ´ÐÐij¸ö´úÂë¿é£¬µ±Ö¸¶¨µÄÌõ¼þÎªÕæ[true]ʱ£¬¼ÌÐøÖØ¸´Ö´ÐУ»Ö±µ½Ìõ¼þΪ¼Ù[false]ʱ£¬Í£Ö¹Ö´ÐУ»
  • for - loops through a block of code a specified number of times
    for£ºÖƶ¨Ñ­»·Óï¾äÖ´ÐеĴÎÊý£»
  • foreach - loops through a block of code for each element in an array
    foreach£ºÑ­»·Ö´ÐÐÒ»¸öÊý×éÖÐÿ¸öÔªËØµÄ´úÂ룻

The while Statement
WhileÓï¾ä

The while statement will execute a block of code if and as long as a condition is true.
WhileÓï¾äÖ¸¶¨ÁËÑ­»·Óï¾äÖ´ÐеÄÌõ¼þ£¬Ö»ÒªÌõ¼þÎªÕæ[true]£¬Ëü¾Í»áÖ´ÐÐÏàÓ¦µÄ´úÂë¿é¡£

Syntax
Óï·¨

while (Ìõ¼þ)
Ö´ÐдúÂë;

Example
°¸Àý

The following example demonstrates a loop that will continue to run as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs:
ÔÚÏÂÃæµÄ°¸ÀýÖУ¬µ±±äÁ¿iСÓÚµÈÓÚ5ʱ£¬Ñ­»·Óï¾ä½«³ÖÐøµÄÖ´ÐУ»Ã¿Ö´ÐÐÒ»¸öÑ­»·£¬iµÄÖµ×Ô¶¯¼Ó“1”¡£

<html>

<body>
<?php 
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>


The do...while Statement
do...whileÓï¾ä

The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.
Èç¹ûʹÓÃdo...whileÓï¾ä£¬ÄÇôָ¶¨µÄ´úÂë¿é½«ÖÁÉÙ±»Ö´ÐÐÒ»´Î£»µ±whileºóÃæµÄÌõ¼þÎªÕæÊ±£¬Ëü½«Öظ´Ö´ÐиÃÖ¸¶¨µÄ´úÂë¿é£º

Syntax
Óï·¨

do
{
Ö´ÐдúÂë;
}
while (Ìõ¼þ);

Example
°¸Àý

The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 5:
ÏÂÃæµÄÀý×Ó½²Ö´ÐÐiµÄÖµÖÁÉÙÒ»´Î£»Ö»ÒªiµÄֵСÓÚ5£¬Õâ¸ö´úÂë¶Î½«±»Öظ´Ö´ÐУº

<html>
<body>
<?php 
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>


The for Statement
ForÓï¾ä

The for statement is used when you know how many times you want to execute a statement or a list of statements.
ForÓï¾äÊÇÓÃÀ´ÉèÖÃÑ­»·Óï¾äµÄÖ´ÐдÎÊýÒÔ¼°Óï¾äÁбíµÄ¡£

Syntax
Óï·¨

for (³õʼֵ; Ìõ¼þ; ÔöÖµ)
{
Ö´ÐдúÂë; }

Note: The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop. If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.
×¢Ò⣺ForÓï¾äÖпÉÒÔÉèÖÃÈý¸ö²ÎÊý£ºµÚÒ»¸ö²ÎÊýÊDZäÁ¿µÄ³õʼֵ£¨³õʼ²ÎÊý£©£»µÚ¶þ¸ö²ÎÊýÊÇÑ­»·Ö´ÐеÄÌõ¼þ£¨Ìõ¼þ²ÎÊý£©£»µÚÈý¸ö²ÎÊýÉèÖÃÁËÔöÁ¿²ÎÊýÔÚÖ´ÐÐÒ»´ÎÑ­»·ÐèÒª×Ô¶¯Ôö¼ÓµÄÁ¿£¨ÔöÁ¿²ÎÊý£©¡£Èç¹û³õʼ²ÎÊý»òÔöÁ¿²ÎÊý²»Ö¹Ò»¸öʱ£¬ËüÃÇÖ®¼äÐèÒªÓöººÅ“,”¸ô¿ª¡£Ìõ¼þ²ÎÊý±ØÐë¿ÉÒÔ·µ»ØÒ»¸öÂß¼­²ÎÊý£¨¼´£ºÕætrue/¼Ùfalse£©¡£

Example
°¸Àý

The following example prints the text "Hello World!" five times:
ÏÂÃæÕâ¸ö°¸ÀýÊä³ö“Hello World!”Îå´Î£º

<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?>
</body>
</html>


The foreach Statement
ForeachÓï¾ä

The foreach statement is used to loop through arrays.
ForeachÓï¾äÊÇÓÃÀ´¶ÔÊý×éÖ´ÐÐÑ­»·µÄ¡£

For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop, you'll be looking at the next element.
¶ÔÓÚÿһ¸öÑ­»·Óï¾äÀ´Ëµ£¬µ±Ç°µÄÊý×éÔªËØ¶¼±»Ö¸ÅÉÁËÒ»¸öÖµ£¨$value£©——ËùÒÔÔÚÏÂÒ»¸öÑ­»·ÖУ¬Ä㽫¿´µ½ÏÂÒ»¸öÔªËØ¡£

Syntax
Óï·¨

foreach (Êý×é as Öµ)
{
Ö´ÐдúÂë; }

Example
°¸Àý

The following example demonstrates a loop that will print the values of the given array:
ÏÂÃæµÄ°¸Àý½«Ñ­»·Êä³ö¸ø¶¨Êý×éÖеÄËùÓбäÁ¿Öµ£º

<html>

<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>

ÆÀÂÛ (0) All