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…

How to implement A2F using Own Development in NodeJS in a Scriptcase application

A2F using Own Development in NodeJS A2F: In a previous article, we talked about Swivel as an extern...

Specialists or generalists, who gets a better advantage by using Scriptcase?

Hey there! Today  we will figure what are the ideal profiles for those who wish to take advantage o...

Disabling autofill and autocomplete Chrome browser in login screens

Hello, here I want to show an alternative to disable the autofill and autocomplete of the chrome bro...

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.