Hướng dẫn for step 2 php

[PHP 4, PHP 5, PHP 7, PHP 8]

for loops are the most complex loops in PHP. They behave like their C counterparts. The syntax of a for loop is:

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 true, the loop continues and the nested statement[s] are executed. If it evaluates to false, the execution of the loop ends.

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 true, like C]. This may not be as useless as you might think, since often you'd want to end the loop using a conditional break statement instead of using the for truth expression.

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 for loops comes in handy in many occasions.

PHP also supports the alternate "colon syntax" for for loops.

for [expr1; expr2; expr3]:
    statement
    ...
endfor;

It's a common thing to many users to iterate through arrays like in the example below.

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[]:

matthiaz

10 years ago

Looping through letters is possible. I'm amazed at how few people know that.

for[$col = 'R'; $col != 'AD'; $col++] {
    echo $col.' ';
}

returns: R S T U V W X Y Z AA AB AC

Take note that you can't use $col < 'AD'. It only works with !=
Very convenient when working with excel columns.

nzamani at cyberworldz dot de

21 years ago

The point about the speed in loops is, that the middle and the last expression are executed EVERY time it loops.
So you should try to take everything that doesn't change out of the loop.
Often you use a function to check the maximum of times it should loop. Like here:



Faster would be:



And here a little trick:



The $i gets changed after the copy for the function [post-increment].

Andrew

8 years ago

You can use strtotime with for loops to loop through dates

Warbo

8 years ago

Remember that for-loops don't always need to go 'forwards'. For example, let's say I have the following code:



However, if the order the looping doesn't matter [ie. each iteration is independent] then we don't need to use an extra variable either, we can just count down [ie. loop 'backwards'] instead:



In fact, we can simplify this even more, since "$i > 0" is equivalent to "$i" [due to type casting]:



Finally, we can switch to a 'pre-decrement' instead of a 'post-decrement' to be slightly more efficient [see, for example, //dfox.me/2011/04/php-most-common-mistakes-part-2-using-post-increment-instead-of-pre-increment/ ]:



In this case we could also replace the entire loop with a map, which might make your algorithm clearer [although this won't work if calculateLoopLength[] == 0]:

ju dot nk at email dot cz

4 years ago

Please note that following code is working:

for [$i=$reverse?[$N-1]:0; $reverse?[$i>-1]:[$i

[This is a modified form of Duff's original device, because PHP doesn't understand the original's egregious syntax.]

That's algorithmically equivalent to the common form:



$val++ can be whatever operation you need to perform ITERATIONS number of times.

On my box, with no users, average run time across 100 samples with ITERATIONS = 10000000 [10 million] is:
Duff version:       7.9857 s
Obvious version: 27.608 s

mparsa1372 at gmail dot com

1 year ago

This example counts to 100 by tens:

epicxmoe at gmail dot com

5 years ago

Adding Letters from A to Z inside an array.

You should test it out.






epicxmoe at gmail dot com

5 years ago

I've tried to search for a results on internet for a basic array which contain letters A to Z inside






Chủ Đề