Home » How To Break Nested Loops In PHP Or Laravel

How To Break Nested Loops In PHP Or Laravel

Last updated on May 23, 2021 by

At some point in the programming, we face many challenges. Here we come up with a simple challenge to break nested loops in PHP or Laravel.

We often use the break statement to break the loop or condition but we often used this statement only for the single loop but not for multiple loops or conditions. In this tutorial, we will see how to break nested loops or conditions in PHP or Laravel. Let’s just jump into it.

Break Nested Loops

To break nested loops, we need to understand the break statement properly. We normally use the break statement alone without any argument but the break statement accepts one optional numeric argument and we are not aware of it.

This argument tells it how many nested enclosing structures are to be broken out of. The default value is 1 that’s why it only breaks the single loop/condition/statement. Let’s understand it by syntax and examples.


break <number of loop/condition/statement to be broken>


break 2;

The above example will broke the 2 nested structures. Let’s take a loop example.

Break Multiple Loops In PHP Or Laravel

In this example, we will try to break the 2 loops using the break statement.


for ( $i=1; $i<=5; $i++ ) { 

	for ( $j=1; $j<=3; $j++ ) {

    	if ( $i === 3) {
	         break 2;

echo 'i = '.$i;

In the above example, we have used the 2 loops. We want to break the loop when variable i value reached to 3. That’s why we have added the if condition and added a break statement with argument 2 which will break the two loops.

Example: Break Loops With Switch Statement In PHP Or Laravel


    $i = 0;
    while (++$i) {
        switch ($i) {
            case 5:
                echo "At 5<br />\n";
                break 1;  /* Exit only the switch. */
            case 10:
                echo "At 10; quitting<br />\n";
                break 2;  /* Exit the switch and the while. */

You can see in the above example, we have used two break statements. The first one in line number 8 will exit only from the switch statement while the second one on line number 11 will exit from the switch and while both.

Additionally, read our guide:

  1. How to Select Data Between Two Dates in MySQL
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How to Convert Base64 to Image in PHP
  12. Check If A String Contains A Specific Word In PHP
  13. Dynamically Populate A Select Field’s Choices In ACF
  14. How To Find Duplicate Records in Database

That’s it for now. We hope this article helped you to break nested loops in PHP or Laravel.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!


Leave a Comment