Which loop is better between for and while loop ?
I need to know it for performance optimization.
Thank's a lot.
Which loop is better between for and while loop ?
I need to know it for performance optimization.
Thank's a lot.
Do not try to tune performance on such a low level, write good and clean code and choose the loop that fits your use case. There will be no significant performance difference between for
and while
anyways, but that does not change the philosophy: do not butcher your code to tune performance.
None is better. You shouldn't even think about such base constructions performance. If you use loops right way, all would be good enough.
Using :
array array_map ( callable $callback , array $array1 [, array $... ] )
is better from both.
<?php
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>
This makes $b have:
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)