A comparison of for, while, and foreach in 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. This is the time to use the PHP loop. PHP provides 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;}
<?phpfor ($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) {    code to is executed;}
<?php//If you had a array with fruit names and prices on you could use Foreach$fruit = Array (     "Orange" =--> "5 . ",     " apple "=" 2.50 ",     " banana "and" 3.99 "), foreach ($fruit as $key + $value) {  " $key is $value D Ollars ";} ?>

Comparison of three types of cycles

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

Let's start by comparing the experiments.

While loop vs. for loop

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

Vs.

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

The above experiment proves that while loops are 19.71% more efficient than for-loop execution. Therefore, it is recommended to use a while loop instead of a for loop whenever 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!

Conclusion

These loops are often used to achieve different purposes, and now we know the performance of each loop in the execution efficiency. When we need to pursue the efficiency of execution, we usually recommend using the while loop instead of the For loop. Also, use the Foreach loop as much as possible between the Foreach loop and the loop loop. Next, we'll look at how to effectively use loops in the template. Please keep your eye on it.


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.