In programming it is common to various ways of solving the same problem, and we show one of the following commands illustrate well this kind of situation. When a variable that needs to check a few interaction options have one of the first commands thinking for solving the problem would be the IF.

In the example below we have the following situation, depending on the value it needs to be performed a certain action.

<?php

$var = 2;

if ($var = 1)

{

echo(“Value is 1.”);

}elseif ($var = 2)

{

echo(“Value is 2.”);

}

?>

If necessary add more values ​​that structure nested IFs would be much higher. In order to optimize the lines of code and hence the processing time, in these cases it is recommended to use the SWITCH command.

<?php

$var = 2;

switch ($var)

{

case 0:

echo(“Value is 0.”);

break;

case 1:

echo(“Value is 1.”);

break;

case 2:

echo(“Value is 2.”);

break;

}

?>

Using the above syntax is possible to write a simple code that will determine the action to be performed according to the value of the variable. The BREAK command is so that on completion of the SWITCH is also finalized since unlike IF, SWITCH runs until the last conditional and so is not a true condition. For there to be completion of the command is used in the conditional BREAK satisfied.

Still within the framework of the SWITCH is the default command that performs a certain action if the variable does not satisfy any condition implemented, as shown below:

<?php

$var = 5;

switch ($var)

{

case 0:

echo(“Value is 0.”);

break;

case 1:

echo(“Value is 1.”);

break;

case 2:

echo(“Value is 2.”);

break;

default:

echo(“Value isn´t 1,2 or 3.”);

break;

}

?>

The SWITCH command can be used to validate any data, so it is widely used in everyday programmers and those who are at the beginning of their studies and need to understand its operation.

php

Anyone starting the program also needs a development tool and practice with many resources to learn quickly and start having it return as soon as possible, so invest in Scriptcase. Download now the free version of our web development tool and get started today to take advantage of all the benefits it can offer. You also relies on section E-Learning with various study materials.

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.