How to Install Operating System ??Complete Guide Installing The Operating System

How to Use Switch Case in PHP

In this tutorial we will learn how to use a switch case in PHP, basically, this switch case is more like the logic of if else, which is for conditioning, but different uses and methods.

Before going forward, there are a number of articles that you might be looking for.

READ TOO :

As said that the PHP switch case is similar to the use of if else, which is for branching where the application checks for a variable condition or the result of calculating expressions and takes decision actions with the order given if it meets the appropriate conditions.

Switch Case in PHP
Switch Case in PHP

Writing Switch Case Logic

The following is an example of using a logic switch case:

<?php
switch (variable)
{
   case 'value1':
      #statement ...
      break;
   case 'value2':
      #statement ...
      break;
   default:
      #statement ...
      break;
}
?>

How does the PHP switch casework? The following is a further explanation of how the switch process can execute commands that are executed.

  1. First, the program checks the value of the variable on the switch.
  2. Next, the program will execute each case whether it matches the value of the given variable.
  3. If the variable value is in one case, the execution process will be stopped with the command break.
  4. If the variable value is not found in one of the cases, the process will end at the default command.

The basic difference in the use of switch cases and if else is the condition of expression, if on if it can hold more than one condition then the switch case is the opposite, that is, it can only accommodate one condition.

But in his statement, the use of a switch case is only able to handle cases that can be executed simultaneously, as simple as using this condition as giving an OR condition if, following the sample code:

<?php
switch (variable)
{
    case 'value1':
    case 'value2':
       #statement ...
       break;
    case 'value 3':
       #statement ...
       break;
    default:
       #statement ...
       break;
}
?>

From the pseudo-code above, we can see that the program will check each value according to the conditions specified, but in “case values 1 and case values 2” will be executed simultaneously, whether the variable has an appropriate value between one values1 and values2, then if yes the statement below will be processed and will stop, but if not then the values 1 and the values will be skipped and will execute the next case.

Switch Case in PHP
Switch Case in PHP

Writing Switch Case Logic in PHP

Then how about writing the switch case logic on PHP? as the author said, this switch logic is similar to if-else, so it should be easy to read the previous article.

The following is an example of writing a switch case logic in PHP:

<?php
$rudi = 'man';
switch ($rudi)
{
    case 'man':
       echo 'Rudi is a man';
       break;
    case 'woman':
       echo 'Oh it turns out that rudi is a woman';
       break;
    default:
       echo 'Introduce my name rudi';
       break;
}
?>

If seen from the code above, as in the previous explanation, the program will check the value given to the $rudi variable, then the program will execute each case, whether it fulfills the specified value, if yes then the statement will be processed, whereas if the variable does not have an appropriate value in each case, the program will execute the default command, or more like else in if else.

The use of a switch case in PHP actually has one advantage compared to using if else, which is to execute conditions faster, but the disadvantage is that the switch case cannot hold more than the condition value in one case.

Before proceeding, there are a number of articles that might be looking for.

ALSO READ :

Sending
User Review
5 (1 vote)

Add a Comment

Your email address will not be published. Required fields are marked *