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…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

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.