w3pop.com :: ÍøÂçѧԺ :: PHP :: PHP Ñ»·
Looping statements in PHP are used to execute the same block of code a specified number of times.
PHPÑ»·Óï¾äµÄ×÷ÓÃÊÇ£º¶Ôͬһ¸ö´úÂë¿éÖ´ÐÐÖ¸¶¨µÄ´ÎÊý¡£
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ÖеÄÑ»·Óï¾ä¾ßÌåÖ¸ÁîÈçÏ£º
The while statement will execute a block of code if and as long as a condition is true.
WhileÓï¾äÖ¸¶¨ÁËÑ»·Óï¾äÖ´ÐеÄÌõ¼þ£¬Ö»ÒªÌõ¼þÎªÕæ[true]£¬Ëü¾Í»áÖ´ÐÐÏàÓ¦µÄ´úÂë¿é¡£
while (Ìõ¼þ) |
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> <?php </body> |
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ºóÃæµÄÌõ¼þÎªÕæÊ±£¬Ëü½«Öظ´Ö´ÐиÃÖ¸¶¨µÄ´úÂë¿é£º
do |
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> <?php </body> |
The for statement is used when you know how many times you want to execute a statement or a list of statements.
ForÓï¾äÊÇÓÃÀ´ÉèÖÃÑ»·Óï¾äµÄÖ´ÐдÎÊýÒÔ¼°Óï¾äÁбíµÄ¡£
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£©¡£
The following example prints the text "Hello World!" five times:
ÏÂÃæÕâ¸ö°¸ÀýÊä³ö“Hello World!”Îå´Î£º
<html> <?php </body> |
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£©——ËùÒÔÔÚÏÂÒ»¸öÑ»·ÖУ¬Ä㽫¿´µ½ÏÂÒ»¸öÔªËØ¡£
foreach (Êý×é as Öµ) |
The following example demonstrates a loop that will print the values of the given array:
ÏÂÃæµÄ°¸Àý½«Ñ»·Êä³ö¸ø¶¨Êý×éÖеÄËùÓбäÁ¿Öµ£º
<html> <?php foreach ($arr as $value) </body> |
ÆÀÂÛ (0)
All