Some time ago I have been programming in php. And I had the following doubt regarding the efficiency of the resources and the speed of how they behave.
My question is this:
What function is more effective when traversing an array?
I was reading documentation where it indicates that foreach
is done to traverse array. But the question is rather which has greater effectiveness, either because it accesses fewer methods or because it is much more native.
I leave an example to make it easier to understand:
Example For:
<?php
$colors = array("red", "green", "blue", "yellow");
$cantidad = count($colors);
for ($i = 0; $i <= $cantidad $i++) {
echo $colors[$i]."<br>";
}
?>
Foreach example:
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo $value."<br>";
}
?>
I appreciate the interest!
NOTE:
I am aware that at the level of only a couple of data, this is not reflected. But the idea is to present it at the level of many data
NOTE2:
As part of a response to the process slowing down if in my cycle for
put count($colors)
I decided to separate it so that only the for itself is measured, and not enter unnecessary methods.