Which loop is faster in PHP?

    Home Technology Which loop is faster in PHP?

    As far as my knowledge is concerned, I never use it almost for array traversal. I will do it for other types of iterations. But for each is just too easy. The execution time difference is very minimal in most scenarios.

    It is very surprising things to watch here:

    for($i =0; $i < count($array); $i++){

    This loop is a very expensive loop. it calls count on every single iteration.

    As for the iterators, for each is equivalent to:

    $key = $it->key();// If using the $key => $value syntax
    $value = $it->current();
    // Contents of the loop in here

    As far as faster ways to iterate is concerned, it just depends on the problem.

    Remember, Premature Optimization Is The Root Of All Evil.

    This is the below example, based on the practical executions time taken;

    $a = array();
    for($i =0; $i <10000; $i++){
    $a[]= $i;
    $start = microtime(true);
    foreach($a as $k => $v){
    $a[$k]= $v +1;
    echo “Completed in “, microtime(true) $start,” Seconds\n”;
    $start = microtime(true);
    $start = microtime(true);
    foreach($a as $k =>&$v){
    $v = $v +1;
    echo “Completed in “, microtime(true) $start,” Seconds\n”;
    $start = microtime(true);
    foreach($a as $k => $v){}
    echo “Completed in “, microtime(true) $start,” Seconds\n”;
    $start = microtime(true);
    foreach($a as $k =>&$v){}
    echo “Completed in “, microtime(true) $start,” Seconds\n”;
    output :

    This is another example of providing script execution time.

    By using a for loop :
    for($i=0; $i<100000; $i++)
    //Do Something
    Using a while loop

    //do something
    Using a do while loop :
    //do something

    } while($i<100000);



    If you are looking for more php online tutorials, Then click the below link :



    Also Read:

    Share Post

    Contributing Author gives you an awesome opportunity to submit your unique content that are educative, informative, knowledgeable and adding value to the people. You may submit the post using the 'Guest Blog' link. Read the guidelines before submission. Thank you.

    Latest Blogs

    Leave a Reply

    1. I like what you guys are up too. Such smart work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my website :).

    2. Terrific article! This is the type of information that are supposed to be shared around the web. Shame on the search engines for not positioning this post upper! Come on over and discuss with my site . Thank you =)

    3. Hi! Someone in my Facebook group shared this site with us so I came to look it over. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Superb blog and outstanding style and design.


    Please enter your comment!
    Please enter your name here

    Recent Comments


    Guidelines To Find the Best Works Boots

    Are you a working individual? Are you searching for snug footwear for your working hours? No need to worry about anything and continue your...

    Resources That Can Help New Families Financially

    A new baby is an exhilarating adventure for any family, yet it can also be accompanied by serious financial struggles. Fortunately, many employers have...

    Is moving during the holidays a good...

    Whether you are moving for a job, the military, college or even love, it is going to be stressful and it will require a...

    Moving to a foreign country for work

    Having a stable job and the ability to provide well both for yourself and your family is one of the most important things today....

    Ship safely and in style with Custom...

    If quality is your motto and you wish to pass it on effectively then these boxes provide a worthwhile opportunity to increase your prominence...


    Subscribe to stay updated.

    Sharing is Caring!

    Help spread the word. You're awesome for doing it!