php - Is it ok to use count($arr) in loop conditions
Get the solution ↓↓↓I am wondering which way is better to use and why?
$arr = array(); // some array
for($i = 0; $i < count($arr); ++$i) {
//some code
}
or
$arr = array(); // some array
$cnt = count($arr);
for($i = 0; $i < $cnt; ++$i) {
//some code
}
Thanks for your answer!
Answer
Solution:
I think the biggest difference what you have to be aware of is in the first example if you modify the array.
As an example:
first:
<?php
$arr = [1,2,3];
for($i = 0; $i < count($arr); ++$i) {
//^^^^^^^^^^^ See here
echo "x". "<br />";
array_pop($arr);
}
?>
Output:
x
x
Condition:$i < count($arr)
1. Iteration: 0 < 3 -> TRUE
2. Iteration: 1 < 2 -> TRUE
3. Iteration: 2 < 1 -> FALSE
second:
<?php
$arr = [1,2,3];
$c = count($arr);
for($i = 0; $i < $c; ++$i) {
//^^ See here
echo "x". "<br />";
array_pop($arr);
}
?>
Output:
x
x
x
Condition:$i < $c
1. Iteration: 0 < 3 -> TRUE
2. Iteration: 1 < 3 -> TRUE
3. Iteration: 2 < 3 -> TRUE
4. Iteration: 3 < 3 -> FALSE
So as you can see in the first example the loop runs 1 iteration less than the other one! Why? Because it checks the condition in every iteration, so when you modify the source array now in the for loop the count will be different in every iteration opposed to that in the second example you count the array before the loop and the variable won't change in the for loop.
Besides this I think it's more a personal preference what you choose. (And if you want the most efficient one go for the second one where you only callcount()
once)
And if you want to take a look at the generated opcode of both examples, then the biggest difference is, that the second one makes 1 operation more than the first one where you don't assign the return ofcount()
.
first:
number of ops: 11
compiled vars: !0 = $arr, !1 = $i
line # * op fetch ext return operands
second:
{-code-12}
Answer
Answer
Answer
Solution:
If you are not hunting for the last millisec runtime, the first example is the more typical one. But it does check the size of the array with each iteration again, hence it is a little slower. But the code is nicer to read. So the first one is programmer book compatible, while the second one is already optimized (probably too early ;)
Answer
Solution:
count is function which called to calculate the number of element in array and you should not call a function repeatedly. if space is not issue then you should call this function one time and store returned value and then use that variable where you want.
- Know about Count
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: to enable extensions, verify that they are enabled in your .ini files
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.