A comparison of for, while, foreach loops in PHP ____php

Source: Internet
Author: User

In the process of using PHP as a programming language, we often encounter situations where a piece of code needs to be executed multiple times. Then you need to use the PHP loop. PHP offers three different types of loops for you to use in the right scenario: For loop while loop Foreach loop for Loop

The For loop is used to determine how many times your expression needs to be executed.

Grammar:

for (initialization condition; increment)
{code to is
    executed;
}
<?php for
($i =1 $i <=100000; $i + +)
{
    echo "the number is". $i. "<br>";
>
While Loop

The while expression executes a piece of code until the conditional statement is false. While loops are generally more appropriate for database-related operations.

Grammar:

while (condition)
{The code to is
    executed;
}
<!--? php
//If you had a array with fruit names and prices in your could use foreach

$fruit = Array ( 
    "orang E "=-->", "5.00", 
    "apple" => "2.50", 
    "Banana" => "3.99" 
); 

foreach ($fruit as $key => $value) { 
    "$key is $value dollars 
";
} 

? >
comparison of three kinds of loops

We know there are multiple loops in PHP, and now we need to know which loops are more efficient so that we can write more quickly.

Let's start the experiment to compare. While Loop vs. for Loop

<?php
    //While loop
    $a =0;
    while ($a < 1000)
    {
        $a + +;
    }
? >

Vs.

<?php
    //For loop for
    ($a = 0; $a < 1000;)
    {
        $a + +;
    }
? >

The above experiments show that while loops are 19.71% more efficient than for-loop execution. Therefore, it is recommended to use the while loop instead of the For loop as much as possible. For loop vs foreach Loop

<?php
    $test = Array (1 => "cat", "dog" => 0, "Red" => "green", 5 => 4, 3, "Me");
    $keys = Array_keys ($test);
    $size = SizeOf ($keys);
    for ($a = 0; $a < $size; $a + +)
    {
        $t = $test [$keys [$a]]
    ;
>

Vs.

<?php
    $test = Array (1 => "cat", "dog" => 0, "Red" => "green", 5 => 4, 3, "Me");
    foreach ($test as $t) {
    
    }
?>

The above experiment proves that the Foreach loop is 141.29% faster than the FOR loop! Conclusions

These loops are often used for different purposes, and now we know the performance of each cycle in execution efficiency. When it comes to execution efficiency, it is often recommended that you use a while loop instead of a for loop. Similarly, between the Foreach loop and the loop loop, use the Foreach loop as much as possible. Next, we'll look at how to effectively use loops in the template. Keep your eye on it.


Original: Http://www.codeproject.com/Tips/1078835/Loops-in-PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.