Hướng dẫn for step 2 php
(PHP 4, PHP 5, PHP 7, PHP 8) for (expr1; expr2; expr3) statement The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to At the end of each iteration, expr3 is evaluated (executed). Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part. expr2 being empty means the loop should be run indefinitely (PHP implicitly
considers it as Consider the following examples. All of them display the numbers 1 through 10:
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 PHP also supports the alternate "colon syntax" for for (expr1; expr2; expr3): statement ... endfor; It's a common thing to many users to iterate through arrays like in the example below.
$i = 0; $i < count($people); ++$i) { The above code can be slow, because the array size is fetched on every iteration. Since the size never changes, the loop be easily optimized by using an intermediate variable to store the size instead of repeatedly calling count():
$i = 0, $size = count($people); $i < $size; ++$i) { matthiaz ¶ 10 years ago
nzamani at cyberworldz dot de ¶ 21 years ago
Andrew ¶ 8 years ago
Warbo ¶ 8 years ago
other comments have pointed out, if "calculateLoopLength" will keep giving back the same value, it can be moved outside the loop:php ju dot nk at email dot cz ¶ 4 years ago
dx at e-mogensen dot dk ¶ 5 years ago
JustinB at harvest dot org ¶ 17 years ago
Philipp Trommler ¶ 9 years ago
= array(0 => "a", 1 => "b", 2 => "c", 3 => "d"); AoKMiKeY ¶ 8 years ago
Vincenzo Raco ¶ 8 years ago
= array( Anonymous ¶ 2 years ago
user at host dot com ¶ 18 years ago
ju dot nk at email dot cz ¶ 4 years ago
htroyo ¶ 6 years ago
Anonymous ¶ 8 years ago
bishop ¶ 19 years ago
mparsa1372 at gmail dot com ¶ 1 year ago
epicxmoe at gmail dot com ¶ 5 years ago
epicxmoe at gmail dot com ¶ 5 years ago
|