[PHP] Switches

Dayron1234

Rapnameiszero,cuzIhavezero,toleranceforidiots
Jun 30, 2010
772
35
Hi everyone, I have a problem with understanding how switches work. I tried to understand using codecademy but I just couldn't understand. I know that switch statements come in handy for having multiple if/elseif/else statements, but everything about switch statements are what I can't understand. I was wondering if anyone can explain it better.
 

Sean

‫‫‫‫‫‫  ‫  Don't Worry, Be Happy
Dec 12, 2011
1,121
405
As m0nsta. said, if statements are faster, but switch statements to look neater and are easier to read.

Here is an example, which would replace simple if statements comparing colors.

PHP:
<?php
 
$color = "blue";
 
if($color == "blue") {
    // Do something if the color is blue
}
else if($color == "red") {
    // Do something if the color is red
}
else if($color == "green") {
    // Do something if the color is green
}
?>

PHP:
<?php
 
    $color = "blue";
 
    switch($color) {
    case "blue":
        // Do something if the color is blue
        break;
    case "red":
        // Do something if the color is red
        break;
    case "green":
        // Do something if the color is green
        break;
    }
 
 
?>
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
if and switch are different statements. The switch can meet one condition and goes through all cases until the condition is met. If statements can meet multiple conditions which need to be met and you have the possibility of doing something else, if the first conditions isn't met.

It depends on what you wan't to do. Don't think about performance unless you got more than 1000 conditions to go through. SeanDavies' example is showing you usages.

For your question about how switches work - it works like a... switch.

Imagine a switch.
It has 5 states.
If we switch to Yellow we produce the color Yellow
If we switch to Green or Red we can produce anything else.
Else If the color is black or anything else we produce something else.

PHP:
switch($color) {
case "Blue":
// $color is Blue we break here. We don't fall through it.
break;
case "Yellow":
// $color is Yellow
// If we return it ends the execution.
return;
case "Green":
case "Red":
// $color is either Green or Red
break;
case "Black":
default:
// $color is either Black or something else
// We can use default if no case is met.
// We can also fall through cases. Useful.
break;
}

Equivalent if statement

PHP:
if($color === "Blue") {
    // $color is Blue.
} else if ($color === "Yellow") {
    // $color is Yellow.
    return;
} else if ($color === "Green" || $color === "Red") {
    // $color is either Green or Red
} else {
    // $color is something else (Notice!)
}
 

thomaslackner

New Member
Jul 1, 2013
1
0
While on the subject of switch statements, another cool thing to try out is using an ARRAY to act as a switch statement or a big pile of IF statements. That sounds crazy, but imagine this:


PHP:
switch ($color_number) {
case 1:
  return 'red';
  break;
case 2:
  return 'green';
  break;
case 3:
  return 'blue';
  break;
}

It's pretty common to use switch statements like this, to map one value to another. But there's actually an easier (and clearer) way:

PHP:
$color_numbers_to_names = array(
  1 => 'red',
  2 => 'green',
  3 => 'blue');
return $color_numbers_to_names[$color_number];

This is great because it makes you think about what *data* your program is working with, not just what the logic is. Often times in large programs, the data contains most of the logic, rather than the code itself (in this example, imagine if you had to make an admin application to allow people to change color number assignments, for example).
 

Users who are viewing this thread

Top