On our blog we talked about the so-called repetition structures which are characterized by commands that run in a loop until a certain condition is satisfied. The syntax of these commands brings all the necessary infrastructure so that the loop works perfectly, but in some situations it is necessary to include certain conditions beyond some of the real function of the structure. When this happens the commands BREAK and CONTINUE end up helping a lot and become the output to be able to implement the necessary logic.

The BREAK command is used to stop the loop before given conditional is met. Consider the example below:

<?php

for ($var= 0; $var <= 100; $var++)

{

echo ($var . ” “);

if ($var%10 == 0)
break;

}

?>

In the example the FOR command is run as instruction to the value 100, but within the structure there is a condition that says if the value is divisible by 10 the command should be paused. This command is mainly used to analyze any kind of exception or error variable so that in the process there is the finishing processing.

The CONTINUE command works in reverse, it performs a check and goes to the next item ignoring the rest of repetition, as in the example below:

<?php

for ($var= 0; $var <= 10; $var++)

{

if ($var%2 == 0)
continue;

echo ($var . ” “);

}

?>

In the above example we have the conditional analyzing only even numbers, every time an odd number goes to check it is printed on the screen through the function echo (), as in the case of the pairs he continues to ignore all functions after him.

php

BREAK and CONTINUE are used in many everyday any programmer, whether beginner or advanced experience. Therefore, understanding them is essential.

You might also like…

AJAX: What it is and how to use it in Scriptcase

Hello, in this post we will conceptually understand the AJAX event and how use  inside the Scriptca...

What advantages for a company in adopting a low-code tool?

Let's understand how low-code tools have become a facilitator in the web development process as they...

How to implement A2F using Swivel in a Scriptcase application

I want to start talking about Swivel, Swivel is a provider of authentication solutions who have been...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.