© 2013 All rights reserved.
2

PHP tip: Do not use functions in end condition of for loop

Function, which is called inside the loop is executed always in each iteration.

 

A lot of programmers use function calls inside the loop, even though it is useless. In some cases it is this necessary, but many programmers use this procedure unnecessarily.

We can look to bad use a for loop:

The execution time of this example is: 1.45 seconds

 

Now we change the example on the following code:

The execution time of this example is: 0.48 seconds

 

This procedure is not available always, for example, if we adjust the variable inside the loop, but it is possible to save CPU time.

Comments are closed for this page

ToM

I think, that article has a bad title. It should be – Do not use functions in end condition of for loop. Because your title advise me – do not use functions inside for loop at all:)

admin

Thank you, you’re right, I modified the title of an article

About
Hi, i am programmer from the Czech Republic. I love web development (Ruby, Ruby on Rails, PHP, Nette) and iOS development (Objective-C, Cocoa).
To cooperate, here is my phone:
+420 608 836