Php Stop Loop and Continue Php Break a Loop

In this guide, we will be showing you how to use the break statement in PHP.

PHP break statement

The break statement allows you can control the flow of loops and the switch statement within PHP.

This statement allows you to stop the execution of the current loop or switch structure. This allows you to break out and continue running the script.

For example, if you find the data you need in a loop, you can break out of it rather than waiting for the loop to complete.

It can be used alongside PHP's continue statement that works by allowing you to skip the current iteration of the loop.

The break statement works with all of the loops supported within PHP, including the for, foreach, while and do-while loops.

Over the next section, we will show you how to utilize the break statement within your PHP code.

Syntax of the break Statement in PHP

Let us start by exploring how exactly the break statement is written within PHP, and any extra functionality it supports.

At its most basic usage, all you need to do is use "break" followed by a semicolon (;). However, PHP does allow you to break out of multiple levels.

By specifying the level, you can break out of a nested loop. This is useful when you hit a case where you need to stop more than just the current loop. We will explore exactly how this works shortly.

When no level is set, the default value will be "1", which is the immediate loop or structure.

Below you can see how the break statement is written in PHP, alongside its optional level field.

              

You can see with this example below how you would write the break statement within a while loop.

              

Additionally, you can see how the level parameter is written when wanting to break out of both the parent and nested loops.

              

Example of using the break Statement in PHP

Now that we have shown you the break statement is written in PHP, we will explore it within a couple of code examples.

We will only be showing the break statement within the for and while loops as it works the same with all other types of loops.

Using the break Statement within a for Loop in PHP

For this first example, let us show how the break statement is used within a for loop in PHP.

Here we have a simple for loop that will count upwards from 0 to 9, incrementing by one on every loop.

We utilize a conditional if statement within this loop to check whether our "$i" variable is identical to 5. If the value equals 5 then PHP will run the break statement, breaking out of the loop.

Otherwise, the value stored in our "$i" variable will be printed by using PHP's echo statement.

              

After running the code above, you can see that our break statement stopped the loop once "$i" was equal to 5.

              

How to use the break Statement in a while Loop

Using the break statement within a while loop in PHP is the same as in a for loop. We will write an infinite while loop that increases a counter on every loop to showcase this.

Since this is an infinite loop, the only way to get out of it is to utilize the break statement.

Within this loop, we increase the value of the "$counter" variable by one on every loop.

We then check whether the value of "$counter" is greater than 5. PHP will run the break statement if the value is greater to exit the loop.

At the end of every loop, we echo the number currently stored in the "$counter" variable.

              

Below is the result you will have gotten after running the PHP code above. You can see that we broke out of the while loop once "$counter" was greater than (>) 5, thanks to PHP's break statement.

              

Break out of a Nested Loop in PHP

Now that we know how PHP's break statement works when used in a single loop let us explore nested loops.

A loop is considered nested when it is run inside another loop. PHP allows you to use the break statement to break out of the current loop and any loop above it.

This works by using "break" followed by the number of levels you want to break out of. With our example below, you can see that our break statement is two levels in so we can use "break 2"

The way this loop is written, it will exit once the value of "$x" plus "$y" is identical.

              

The code above will have produced the following result. With this result, you can see how PHP's break statement jumped the code out of both levels of loops.

              

Using the break Statement Within the switch Structure

The break statement in PHP is also crucial to the switch structure as it allows you to break out of the switch without falling through to the next case.

While we won't be explaining the switch structure in depth here, we will quickly write an example.

With this example, we will create a variable called "$number" that will be assigned the number 2. This value is then passed into our switch structure.

When this value is passed in, the switch will jump to the corresponding "case". With this example, that means it will jump to "case 2".

Since we aren't using a break at "case 2" PHP will fall through to the next block. In this example, you will end up with both the text "Number is 2" and "Number is 3" printed.

If you don't use "break", PHP will continue to fall through to the next case until the switch structure finishes executing.

              

Below, you can see how the switch statement relies on PHP's "break" statement to jump out of the structure. Without it, PHP will fall through to the next case, as showcased below.

              

Conclusion

We have shown you how the break statement is used within PHP throughout this tutorial. It is crucial to controlling the flow of loops as it allows you to break out of them early.

Additionally, the break statement is also crucial to effectively using the case structure within PHP. Using break within this structure allows you to stop it from falling through to the next case.

Please comment below if you have any questions about using break within your PHP script.

You should also check out our many other PHP tutorials to learn more. We also have various other coding guides if you want to explore new programming languages.

barnettawase1936.blogspot.com

Source: https://pimylifeup.com/php-break/

0 Response to "Php Stop Loop and Continue Php Break a Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel