Bonjour ma question et la suivante : Comment bien programmer en PHP ? Donc si vous avez des conseils, des truc à utiliser/éviter et d'autres choses. Par exemple : Code le plus court : Code : for ($i=0; $i<count($array); $i++) { echo $array[$i]; } Code le plus rapide : Code : $count = count($array); for ($i=0; $i<$count; $i++) { echo $array[$i]; } Merci d'avance
I get what you are trying to say but the main language of this forum is English UK/US. I would suggest you to edit your post because a mod may give you an infraction for it. No Offence
hrm... interesting. obviously, it will differ between callback and w/o it. in javascript, for example, the fastest loop can be achieved by a while(count--) { } where count is set to array.length. i coded a test for 4 common ways of walking array elements in PHP, timing how long it takes to process in ms: <?PHP // populate the array for ($ii = 0; $ii < 1000; ++$ii) $array[] = rand(100000,100000+1000); // loop 1, with callback to count function $timeStart = microtime(); for ($i=0; $i < count($array); ++$i) echo $array[$i] . " "; echo "<h1>for loop, with count() callback: " .(microtime() - $timeStart) . " ms</h1>"; // loop 2, count predefined. $timeStart = microtime(); $count = count($array); for ($i = 0; $i < $count; $i++) echo $array[$i] . " "; echo "<h1>for loop, no count() callback: " .(microtime() - $timeStart) . " ms</h1>"; // loop 3. reverse loop $timeStart = microtime(); $count = count($array); while ($count--) echo $array[$count] . " "; echo "<h1>while() reducing: " .(microtime() - $timeStart) . " ms</h1>"; // loop 4, foreach $timeStart = microtime(); foreach($array as $item) echo $item . " "; echo "<h1>foreach loop: " .(microtime() - $timeStart) . " ms</h1>"; ?> PHP: here are my results: for loop, with count() callback: 0.001327 ms for loop, no count() callback: 0.00087000000000001 ms while() reducing: 0.00096599999999999 ms foreach loop: 0.00062400000000001 ms winner: foreach. if anyone else wants to comment on this - etc - i know the results are not conclusive enough and would vary dependent on my server load etc - this needs to run 1000 times and get averages... but i'll be interested in hearing people from their experience.