5

I am using a foreach loop within PHP similar to this:

foreach ($class->getAttributes() as $attribute) {
// Work
}

Concerning efficiency, is it better to have a $attributes = $class->getAttributes(); statement outside the foreach loop and iterate over the $attributes variable? Or is the $class->getAttributes() statement only getting called once inside the foreach declaration at the beginning?

(I realize this might not be a big efficiency concern in this case, but I would like to know the principle for this and other larger cases)

Thanks,

Steve

Gumbo
657k112 gold badges792 silver badges852 bronze badges
asked Feb 27, 2010 at 17:38

4 Answers 4

15

Using $class->getAttributes() outside of the foreach loop and using a temporary variable, or keeping it like you wrote should not change anything about performances : it will still be evaluated only once.

And here is an example that proves it :

function get_array() {
 echo 'function called !<br />';
 return array(
 'first' => 123,
 'second' => 456,
 'last' => 789, 
 );
}
foreach (get_array() as $key => $value) {
 echo "$key : $value<br />";
}

I am using a function and not a method of a class, to get a shorter example, but the principle would be the same with a class+method.


And calling this portion of code gives the following output :

function called !
first : 123
second : 456
last : 789

i.e. the get_array() function is only called once, at the beginning of the foreach loop.

answered Feb 27, 2010 at 17:40
Sign up to request clarification or add additional context in comments.

Comments

2

foreach operates on a copy internally:

Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. [...]

So it doesn’t make any difference in your case. $class->getAttributes() is only called once to retrieve the array.

answered Feb 27, 2010 at 17:42

Comments

1

I haven't tested, but I'd think that doing something like this:

$attributes = $class->getAttributes();
foreach($attributes as $attribute)
{
}

Is more readable, and you can get to the attributes after the foreach has conclude if you need to.

However, in a more direct response to your question, getAttributes() will only be called once in either case.

answered Feb 27, 2010 at 17:40

1 Comment

+1 for the convenience of accessing $attributes outside of the loop.
1

As others have said, the function in your example is only called once.

The case in which pre-evaluating can make a difference is for loops.

$str = 'abcdefghijklmnop';
//strlen will be called on every iteration
for($i = 0; $i < strlen($str); $i++);
//strlen will only be called once
$len = strlen($str);
for($i = 0; $i < $len; $i++);
answered Feb 27, 2010 at 17:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.