홈으로 홈으로 | 무료회원가입 | 아이디/비번찾기 | 즐겨찾기
추천음악방송메인홈1대1상담음악방송청취
아리랑가요(무료음악채널)
뽕짝아가씨(무료음악채널)
okTrot(무료음악채널)
전통가요(무료음악채널)
미스트롯(무료음악채널)

온라이브스토어(앱다운로드)

환상의뽕짝파티 무료음악
명작트로트메들리 무료음악
트로트메들리관광 무료음악
동영상노래방
가사검색

POP Player
신나는 고스톱
컴퓨터 오목
작은 음악다방
자동러시 웹플레이어신청

보안∵서버∵쿠키
윈도우∵프레임
이미지∵배경
시간∵달력∵계산
상태바∵타이틀바
음악∵영상
폼∵전송∵테이블
키보드∵마우스
효과∵링크∵메뉴
Script∵php
기타
PHP 강좌
윈도우관련팁
웹관련팁
홈페이지제작팁
기본강좌
레지스트리
윈도우서버
리눅스서버
Android
PHP switch
8년 전
switch구문은 연속적인 같은 표현식을 갖는 연속적인 IF구문과 비슷하다. 많은 경우, 하나의 변수(또는 표현식)으로 다른 많은 값과 비교할 필요가 있으며, 그 값이 동일한 코드의 파편들을 수행할 필요가 생기게 된다. 정확히 이런 목적을 위해 switch구문이 사용된다.

Note: 다른 언어와는 달리 continue문은 switch문에서 사용할수 있고, break문과 비슷하게 동작한다. 루프 내에 switch문을 포함하고 있고 바깥 루프의 다음 반복문으로 진행하고 싶다면 continue 2를 사용한다.

Note: switch/case는 느슨한 비교를 하는 점에 주의하십시오.


다음 예제 코드들은 같은 일을 서로 다르게 표현한 것입니다. 하나는 if와 elseif문을 사용한 것이고, 다른 하나는 switch문을 사용했습니다:

Example #1 switch 구조

<?php
if ($i == 0) {
    echo "i는 0";
} elseif ($i == 1) {
    echo "i는 1";
} elseif ($i == 2) {
    echo "i는 2";
}

switch ($i) {
case 0:
    echo "i는 0";
    break;
case 1:
    echo "i는 1";
    break;
case 2:
    echo "i는 2";
    break;
}
?>

Example #2 문자열을 사용하는 switch 구조

<?php
switch ($i) {
case "apple":
    echo "i는 apple";
    break;
case "bar"
    echo "i는 bar";
    break;
case "cake":
    echo "i는 cake";
    break;
}
?>

실수하지 않기 위해 switch문이 어떻게 동작하는지 이해할 필요가 있다. switch문은 한줄씩 수행된다 (실제는, 한구문씩). 처음에는 아무 코드도 수행되지 않는다. switch 표현의 값과 일치하는 값을 가진 case 구문을 발견했을 때, PHP는 그 구분을 실행합니다. PHP는 switch블록의 끝부분이 될때까지, 또는 break문와 첫번째 조우를 할때까지 구문을 계속 수행해 간다. 만약 각 case 구문 목록의 끝부분에 break문을 쓰지않는다면 PHP는 다음 case문으로 계속 진행하게 된다. 예를 들면 다음과 같다:

<?php
switch ($i) {
case 0:
    echo "i는 0과 같다";
case 1:
    echo "i는 1과 같다";
case 2:
    echo "i는 2와 같다";
}
?>

여기에서, $i가 0이라면, PHP는 모든 echo문을 실행합니다! $i가 1이라면, PHP는 마지막 두 echo문을 실행합니다. $i가 2일 때만, 원하는 동작('i는 2와 같다' 표시)을 합니다. 따라서, break을 잊어서는 안됩니다. (어떤 경우에는 일부러 빠트릴 수 있어도, 잊지 마십시오)

switch구문에서, 조건문은 오직 한번만 평가되고 각 case문에서 결과가 비교되어진다. elseif문에서는 조건문은 다시 평가된다. 조건문이 한번 이상의 비교가 필요한 복잡한 것이거나 거친(tight) 루프안에 있다면 switch문 좀 더 빠를것이다.

case에 대한 구문 목록은 비어있을수 있다. 이것은 단순히 다음 case문으로 제어권을 넘겨줄 뿐이다.

<?php
switch ($i) {
case 0:
case 1:
case 2:
    echo "i는 3보다 작지만 음수는 아닙니다.";
    break;
case 3:
    echo "i는 3";
}
?>

특별한 case가 바로 default case문이다. 이것은 다른 case문과 모두 조건이 맞지 않을때의 경우를 위한 것입니다. 예를 들면:

<?php
switch ($i) {
case 0:
    echo "i는 0과 같다";
    break;
case 1:
    echo "i는 1과 같다";
    break;
case 2:
    echo "i는 2와 같다";
    break;
default:
    echo "i는 0, 1, 2 어느것도 아니다";
}
?>

case의 표현식은 정수나 부동소수점 수와 문자열같은 단순형으로 평가되는 어던 표현식도 될수 있다. 여기에 단순형으로 재참조(dereference)되지 않는 배열이나 객체를 사용할수는 없다.

switch문을 위한 제어 구조의 대체 문법이 지원된다. 더 자세한 정보는 제어 구조의 대체 문법을 참고.

<?php
switch ($i):
case 0:
    echo "i equals 0";
    break;
case 1:
    echo "i equals 1";
    break;
case 2:
    echo "i equals 2";
    break;
default:
    echo "i is not equal to 0, 1 or 2";
endswitch;
?>

case 뒤에 세미콜론 대신 콜론을 쓸 수 있습니다:

<?php
switch($beer)
{
    case 'tuborg';
    case 'carlsberg';
    case 'heineken';
        echo 'Good choice';
    break;
    default;
        echo 'Please make a new selection...';
    break;
}
?>



declare continue
--------------------------------------------------------------------------------
Last updated: Fri, 24 Jul 2009
  
  add a note User Contributed Notes
switch
theimp at iinet dot net dot au
02-Jun-2010 08:04
It's easy to abuse the switch syntax to do some very useful things. As this example will show, the possibilities can go beyond even Duff's Device-style craziness (not that this example is nearly as clever as Duff's Device, but it demonstrates how you can do certain things other than simply the increment/decrement/assignment that's possible in C).

Fundamentally, this works mostly due to the fact that, from the point of view of the assembler/interpreter, a switch block is hardly any different from a bunch of GOTO labels and  if()  evaluations. But, like an  if() evaluation, the line of a case: statement is evaluated as an expression. So, in this case, we can perform an assignment and match the result of that assignment, because the return value of an assignment is the data that was assigned (and not the value of the variable it was assigned to, as you might expect).

So far, this is not actually amazing, even if it is a bit unintuitive. From a language point-of-view, it would be the same as an  if($var = "string")  statement which is using an assignment (=) rather than a comparison (==) operator. When you look at the pre-processing optimization, because a normal assignment of $var = "string" will always equal "string", it makes sense to have the result of that expression simply be equal to the right side of the expression (the right side is used rather than the left to let the assembler/interpreter work faster, on account of how they traditionally simply change the memory location for the assigned variable rather than copy the memory around unnecessarily).

Where this becomes more interesting is where, in PHP, you have language constructs that behave like functions but are used like statements. An  $array[] = "string"  expression is actually a language construct that just happens to behave a lot like a function, but you use it in the same way that you use an assignment expression, and like an expression, it always evaluates to the right side of the expression; in this case,  "string"  and not  array() .

The assembler/interpreter can't use the right side of the expression as a shortcut for the result of a function, so you can't use functions in this way in a case statement. You also can't get around this limit on calling functions from the case line by using variable functions, because they are used in the same way as functions.

But imagine what you could do with other language constructs, like eval() or include() !

Consider the following:

<?php
function flavor($type = null)
{
    switch ($type) {
        /* Note the isolation of break; statements and the fact that default: is at the top */
        default:
            $type = null;
        case $array[] = "chocolate":
            if ($type != null) {
                $array = array($type);
                break;
            }
        case $array[] = "strawberry":
            if ($type != null) {
                $array = array($type);
                break;
            }
        case $array[] = "vanilla":
            if ($type != null) {
                $array = array($type);
                break;
            }
    }
    if ( (count($array) != 1) ) {
        return "Flavors available: " . implode(", ", $array);
    } else {
        return "Flavor selected: " . implode(", ", $array);
    }
}

echo flavor() . "<br>";
/* Flavors available: chocolate, strawberry, vanilla */

echo flavor("banana") . "<br>";
/* Flavors available: chocolate, strawberry, vanilla */

echo flavor("chocolate") . "<br>";
/* Flavor selected: chocolate */
?>

What makes this example useful is that you don't need a variable somewhere that contains the available options (even within the function itself), so to support new options, you only ever have to change the code to add the new option - you don't need to update some variable somewhere that controls whether or not it works or whether or not people can tell that there's a new option.
mr at bwsolution dot de
11-May-2010 09:29
"loose comparison" means that switch won't check the type.
switch will only compare values:
<?php
if('a string' == 0) echo 'a string is 0' . PHP_EOL;
if('a string' === 0) echo 'but you will never see this' . PHP_EOL;
switch(0){
    case 'a string': echo 'a string' . PHP_EOL;
    case 'another string': echo 'another string' . PHP_EOL;
}

if('a string' == true) echo 'a string is TRUE' . PHP_EOL;
if('a string' === true) echo 'but you will never see this' . PHP_EOL;
switch(true){
    case 'a string': echo 'a string' . PHP_EOL;
    case 'another string': echo 'another string' . PHP_EOL;
}
?>

will output:
a string is 0
a string
another string
a string is TRUE
a string
another string
hackajar yahoo com
01-Apr-2010 11:21
Apparently you need to be extra careful with "0" (zero) in your case statements when mixing int and string values:

switch($key) {
     case 0: echo "only if I am zero";break;
     case 1: echo "I'm one";break;
     case "E1": echo "Why don't you output me?";break;
     default: echo "No value Found";
}

Switch will be convinced that you mean "0" when you really mean "E1" unless you wrap it in quotes:

switch($key) {
     case '0': echo "only if I am zero";break;
     case 1: echo "I'm one";break;
     case "E1":echo "Yay! I'm back!";break;
     default: echo "No value Found";
}

Maybe this is what they mean by "loose comparison" with True, False operators?
lchanady at gmail dot com
27-Jan-2010 05:56
Something fairly simple (and maybe obvious) that I didn't see mentioned is that the default case WILL be executed even if the switched variable does not exist or is undefined.

For example:

<?php

$a = "abc";
$b = "def";

switch($c){
    case "a":
        echo "a";
        break;
    case "b":
        echo "b";
        break;
    default:
        echo "default";
        break;
}

?>

Will output: default

Even though $c was never declared or defined, the default case will still be executed rather than PHP throwing an error.
varnavruz at gmail dot com
04-Sep-2009 09:34
You also may also use that for comparison. For example, we measure the script execution time and generate a comment about it. $totaltime is number of seconds script executes.

<?php

switch ($totaltime) {

case ($totaltime < 1):
echo "That was fast!";
break;

case ($totaltime > 1):
echo "Not fast!";
break;

case ($totaltime > 10):
echo "That's slooooow";
break;
}

?>
Ukuser
01-Aug-2009 02:48
Again, just to re-iterate, if you supply 0 as the switched element, only the first statement will run if the comparison is with text.

<?php

$in = array('first',0,"second");

foreach ($in as $a){
  switch ($a){
    case "first": print "first<br>"; break;
    case "second": print "second<br>"; break;
  }
}

?>

This is annoying if you're using an array where you've got key's which could be text or numbers, so I'm using the suggested idea of:

<?php

switch (true){
  case ($a==="first"): print "first<br>"; break;
  case ($a==="second"): print "second<br>"; break;
}

?>

The reason for this as mentioned on http://uk3.php.net/ternary is that: "If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement." Effectively it's saying if ($a=="first") which becomes does ($a (0) == 0) which is yes.

In my example this mean't a date had a regular expression of an email applied to it which didnt help!
Piyush Patel
24-Jul-2009 07:21
You may also call different functions within switch as per the action you get on the page.

<?php
switch($_POST['action']) {
case 'sum':
  execSum();
  break;
case 'sub':
  execSub();
  break;
case 'mul':
  execMul();
  break;
default:
  defaultAction();
  break;
}
?>
sedativchunk at gmail dot com
02-Feb-2009 05:22
Not sure if this has been posted or not, but I found the switch statement useful for finding ranges of data.

This script creates web 2.0 style links in different font sizes (popular on blogs) using a randomizer and switch statement. I used links from within a database for a mod I made for a Simple Machines forums, but this example uses arrays for links if you wanted to add your own custom links:
<?php
// Create set of links
$link = array();
$link[] = '<a href="whatever.html">page 1</a>';
$link[] = '<a href="whatever.html">page 2</a>';
$link[] = '<a href="whatever.html">page 3</a>';
$link[] = '<a href="whatever.html">page 4</a>';
$link[] = '<a href="whatever.html">page 5</a>';
$link[] = '<a href="whatever.html">page 6</a>';
$link[] = '<a href="whatever.html">page 7</a>';
$link[] = '<a href="whatever.html">page 8</a>';

// Create loop to display links
for($i = 0; $i < count($link); ++$i)
{
    // Create randomizer
    // Use switch statement to find font size
    $randomizer = rand(1,50);
    switch($randomizer)
    {
    case ($randomizer <= 20):
    $font_size = "11";
    break;

    case ($randomizer <= 30):
    $font_size = "16";
    break;

    case ($randomizer <= 40):
    $font_size = "18";
    break;

    case ($randomizer <= 50):
    $font_size = "20";
    break;
    }

    // Display the link
    echo '<span style="font-size: ' .$font_size. ';">' .$link[$i]. '</span>  ';

// Loop the next link
}
?>

Using this type of range randomizer is useful for game development and it can be useful on the web too, for things where you don't want to use a randomizer just for things like (1-5) where you wanted a more then likely result for one thing over another. The switch statement saves from writing spaghetti code if statements.
redcore
17-Jan-2009 12:42
This is what I did while deciphering what quarter a date (string) belonged to...

<?php
$strdate = 'Jun 4 2008 11:34PM';
$month = date('M', strtotime($strdate));

switch ($month)
    {
    case ($month=='Jan' || $month=='Feb' || $month=='Mar'):
        $quarter = "Q1 2008";
        break;
    case ($month=='Apr' || $month=='May' || $month=='Jun'):
        $quarter = "Q2 2008";
        break;
    case ($month=='Jul' || $month=='Aug' || $month=='Sep'):
        $quarter = "Q3 2008";
        break;
    case ($month=='Oct' || $month=='Nov' || $month=='Dec'):
        $quarter = "Q4 2008";
        break;
    }
?>
hamiltont at gmail dot com
02-Jan-2009 07:32
Example of default NOT included as the last item

<?php
switch(5) {
  case 1:
    echo "1";
    break;
  case 2:
  default:
    echo "2, default";
    break;
  case 3;
    echo "3";
    break;
}
?>

Outputs '2,default'

very useful if you want your cases to be presented in a logical order in the code (as in, not saying case 1, case 3, case 2/default) and your cases are very long so you do not want to repeat the entire case code at the bottom for the default

Hamy
Keil
09-Dec-2008 04:06
As follow-up to ben dot lancaster at holler dot co dot uk's post:

'continue' ends the switch, not the case, just as it would with any other flow control. Think of it as putting the execution pointer right before the ending accolade (that is, the }) because that is essentially what happens. In the case of a for loop, this would cause the iteration clause to execute, and if applicable, the loop to begin again. However, switches do not loop, which is why (as noted above, in the manual!) a continue statement essentially acts as a break when within a switch.
richard
10-Nov-2008 09:32
Just a word of warning about using switch don't try and compare variables that contain numbers with strings like so:

<?php
$i=0;

switch($i)
{
    case 'TEST': print "Test";break;
    case 0: print "0";break;
}
?>

The output will be: Test and not 0.
ben dot lancaster at holler dot co dot uk
16-Oct-2008 10:46
Following on from bensphpnetemail at supernaut dot org's post, it would seem that 'continue' doesn't really continue at all. Consider the following:

<?php
$foo = 'bar';
$bar = true;

switch($foo)
{
    case 'bar':
        if($bar)
        {
            continue;
        }
        echo '$bar is false';
        break;

    case 'bar':
    case 'foo':
        echo '$bar is true, or $foo is foo';
        break;
    
    default:
        echo "You shouldnt ever get here";
        break;
}

?>

I would expect the above to output "$bar is true, or $foo is foo", but it doesn't output anything. The continue statement acts as a break and stops evaluating the rest of the matching cases.

Instead, you should issue a 'break' statement conditionally to achieve the desired result.
mar dot czapla at gmail dot com
10-Oct-2008 09:17
<?php
    /* script 1  */
    $foo = "not a number";
    switch(false)
    {
        case "1":    {    $foo = "1";    break;    }
        case "2":    {    $foo = "2";    break;    }
        default:    {    $foo = "0";    }
    }
    
    echo $foo;    // will produce "not a number"
    
    /* script 2  */
    $foo = "not a number";
    $arr = array("not a number"); // 1 element only !
    switch($arr[1])    // element $foo[1] doesn't defined
    {
        case "1":    {    $foo = "1";    break;    }
        case "2":    {    $foo = "2";    break;    }
        default:    {    $foo = "0";    }
    }
    
    echo $foo;    // will produce "not a number" ( not 0 ! )
    
    /* script 3  */
    $foo = "not a number";
    $arr = array("not a number"); // 1 element only !
    switch($arr[1]?$arr[1]:"1")    // element $foo[1] doesn't defined
    {
        case "1":    {    $foo = "1";    break;    }
        case "2":    {    $foo = "2";    break;    }
        default:    {    $foo = "0";    }
    }
    
    echo $foo;    
    // will produce :
    // 1 if $arr[1] isn't set
    // 1 if $arr[1]=1
    // 2 if $arr[1]=2
    // 0 if none of above
?>
cretz
01-Oct-2008 12:56
Haven't seen it mentioned here, but at least in my version (PHP 5.2.5) and I'm sure all of PHP 5, the switch statement is a great way to check type safe enumerates that are otherwise missing in the PHP language. Example:

<?php

class WannabeEnum {
    /**
     * @var WannabeEnum
     */
    public static $FOO;
    /**
     * @var WannabeEnum
     */
    public static $BAR;
    /**
     * @var WannabeEnum
     */
    public static $FOOBAR;
    private $_ordinal;
    public function __construct($ordinal) {
        $this->_ordinal = $ordinal;
    }
}
WannabeEnum::$FOO = new WannabeEnum(1);
WannabeEnum::$BAR = new WannabeEnum(2);
WannabeEnum::$FOOBAR = new WannabeEnum(3);

function testSwitch(WannabeEnum $wannabeEnum) {
    switch($wannabeEnum) {
        case WannabeEnum::$FOO:
            echo('Foo!' . PHP_EOL);
            break;
        case WannabeEnum::$BAR:
            echo('Bar!' . PHP_EOL);
            break;
        default:
            echo('Default!' . PHP_EOL);
    }    
}
testSwitch(WannabeEnum::$FOO);
testSwitch(WannabeEnum::$FOOBAR);
?>

Outputs:

Foo!
Default!

Don't forget it uses loose comparisons!
mitchell/[.]/cain/[at]/gmail/[.]/com
11-Sep-2008 12:52
double switch

<?php

$foo = 2;
$bar = 11;

switch ($foo) {
    case 1:
        echo 'foo is 1//';
        break;
    case 2:
        echo 'foo is 2//';
        switch ($bar) {
            case 10:
                echo 'bar is 10//';
                break;
            case 11:
                echo 'bar is 11//';
                break;
            default:
                echo 'bar is neither 10 or 11//';
                break;
        }
        break;
    default:
        echo 'foo is neither 1 or 2//';
        break;
}

?>

output:
foo is 2//bar is 11//
mmxg at shaw dot ca
01-Jul-2008 08:30
In reply to lko at netuse dot de

Just so others know whom may not, that's because PHP does automatic type conversion if a string is evaluated as an integer (it sees the 2 in '2string' so when compared like if ('2string' == 2), PHP sees if (2 == 2) ).

I just tested it, but if you go:

<?php

$string="2string";

switch($string)
{
    case (string) 1:
        echo "this is 1";
        break;
    case (string) 2:
        echo "this is 2";
        break;
    case '2string':
        echo "this is a string";
        break;
}

?>

The output will be "this is a string" and if you change $string to "2" it will again be "this is 2".

Just in case that may help anyone who may run into that problem.
johan at dotco dot net
07-May-2008 12:51
A switch statement with more than one default is valid in PHP 5.2.6.  However, only the last default gets executed as the default clause.

<?php

switch(9) {
    case 1:
        echo 1 . "\n";
    case 2:
        echo 2 . "\n";
        break;

    case 3:
        echo 3 . "\n";
    case 4:
        echo 4 . "\n";

    case 5:
        echo 5 . "\n";
    default:
        echo 6 . "\n";
    default:
        echo 7 . "\n";
    default:
        echo 8 . "\n";
}

// Outputs:
// 8
//
// Did you expect 6 and 7 to print too?

switch(3) {
    case 1:
        echo 1 . "\n";
    case 2:
        echo 2 . "\n";
        break;

    case 3:
        echo 3 . "\n";
    case 4:
        echo 4 . "\n";

    case 5:
        echo 5 . "\n";
    default:
        echo 6 . "\n";
    default:
        echo 7 . "\n";
    default:
        echo 8 . "\n";
}

// Outputs:
// 3
// 4
// 5
// 6
// 7
// 8

?>

The problem (if it is one), is in the recursive definition of the default clause of a case_list in the parser.

zend_language_parser.y

// Many default clauses allowed.
case_list:
        /* empty */
    |    case_list T_CASE expr case_separator  inner_statement_list
    |    case_list T_DEFAULT case_separator inner_statement_list
    ;

// Only one default clause allowed.
case_list:
        /* empty */
    |    case_list T_CASE expr case_separator  inner_statement_list
    |    T_DEFAULT case_separator inner_statement_list
    ;
jphansen at uga dot edu
11-Apr-2008 06:33
An easy mistake some programmers from other languages might make is delimiting conditions for a case block by a comma:

<?
$val = "x";
switch ($val) {
  case "x", "y", "z":
    echo "Case is x, y or z";
    break;
}
?>

Instead, in PHP this is accomplished by using the "case" identifier for each condition:

<?
$val = "x";
switch ($val) {
  case "x":
  case "y":
  case "z":
    echo "Case is x, y or z";
    break;
}
?>
lko at netuse dot de
07-Apr-2008 03:51
Attention if you have mixed types of value in one switch statemet it can make you some trouble

<?php

$string="2string";

switch($string)
{
    case 1:
        echo "this is 1";
        break;
    case 2:
        echo "this is 2";
        break;
    case '2string':
        echo "this is a string";
        break;
}

?>

The swich-statement will halt on 'case 2'

Answer: this is 2
Hayley Watson
24-Oct-2007 09:02
Something not mentioned in the documentation itself, and only touched on momentarily in these notes, is that the default: case need not be the last clause in the switch.
<?php
for($i=0; $i<8; ++$i)
{
    echo $i,"\t";
    switch($i)
    {
    case 1: echo "One"; break;
    case 2:
    default: echo "Thingy"; break;
    case 3:
    case 4: echo "Three or Four"; break;
    case 5: echo "Five"; break;
    }
    echo "\n";
}
?>
Outputs what you'd expect, namely
0       Thingy
1       One
2       Thingy
3       Three or Four
4       Three or Four
5       Five
6       Thingy
7       Thingy
with case 2 and the default both producing the same result ("Thingy"); strictly speaking, the case 2 clause is completely empty and control just falls straight through. The same result could have been achieved with
<?php
switch($i)
{
    case 1: echo "One"; break;
    case 3:
    case 4: echo "Three or Four"; break;
    case 5: echo "Five"; break;
    default: echo "Thingy"; break;
}
?>
But if "case 2" represented a fairly common case (other than "everything else"), then it would be better to declare it explicitly, not only because it saves time by not having to test EVERY other case first  (in the current example, PHP finds 'case 2' in the first switch in two tests, but in the second switch it has to make four tests before giving up and going with the default) but also because someone (perhaps yourself in a few months' time) will be reading the code and expecting to see it handled. Listing it explicitly aids comprehension
stever at ashomecare dot com
07-Sep-2007 04:11
Just a reminder: there may be easier ways to manipulate _long_ lists of data instead of using switches.

function getChineseZodiac($year){
  
  // Chinese Zodiac Animals
  $animals = Array
  (
    'Monkey',  // Years 0, 12, 1200, 2004...
    'Rooster',
    'Dog',
    'Boar',
    'Rat',
    'Ox',
    'Tiger',
    'Rabit',
    'Dragon',
    'Snake',
    'Horse',
    'Lamb'
  );

  // Number of animals and years in a calendar rotation
  $numAnimals = count($animals);

  // Years left until full rotation of calender
  $yearOffset= round($year) % $numAnimals;
  
  return $animals[$yearOffset];
  
}

Of course this is a really generic function, we're just finding how many years away from a full 12 year rotation the current year is.
fteng1 at gmail dot com
04-Aug-2007 01:46
Here's a lazy way of doing an comparison where multiple conditions equal the same result using arrays.

if (in_array($year,array(1948, 1960, 1972, 1984, 1996, 2008)))
        echo "Rat";
    elseif (in_array($year,array(1949, 1961, 1973, 1985, 1997, 2009)))
        echo "Ox";
    elseif (in_array($year,array(1950, 1962, 1974, 1986, 1998, 2010)))
        echo "Tiger";
    elseif (in_array($year,array(1951, 1963, 1975, 1987, 1999, 2011)))
        echo "Rabbit";
    elseif (in_array($year,array(1952, 1964, 1976, 1988, 2000, 2012)))
        echo "Dragon";
    elseif (in_array($year,array(1941, 1953, 1965, 1977, 1989, 2001)))
        echo "Snake";
    elseif (in_array($year,array(1942, 1954, 1966, 1978, 1990, 2002)))
        echo "Horse";
    elseif (in_array($year,array(1943, 1955, 1967, 1979, 1991, 2003)))
        echo "Lamb";
    elseif (in_array($year,array(1944, 1956, 1968, 1980, 1992, 2004)))
        echo "Monkey";
    elseif (in_array($year,array(1945, 1957, 1969, 1981, 1993, 2005)))
        echo "Rooster";
    elseif (in_array($year,array(1946, 1958, 1970, 1982, 1994, 2006)))
        echo "Dog";
    elseif (in_array($year,array(1947, 1959, 1971, 1983, 1995, 2007)))
        echo "Boar";
juraj5
21-Jul-2007 07:00
In response to 'i luv spam',

when you enter 07, you tell PHP to interpret a number as an octal number (much like '0x' for hex numbers). Octal numbering system uses only 8 digits, i.e. 0-7. http://en.wikipedia.org/wiki/Octal

The number 8 does not exist in octal numbering system. The comparison works because the octal numbers 0 to 7 have identical counterparts in decimal system.

So, in order to get a number compared as decimal 8, you would have to enter 010 in the case.

BTW this behavior obviously isn't specific to switch, it's a part of PHP.

(I personally stumbled into this when trying to make my code nicely indented while declaring an array)
usera$ at example dot com
18-Jul-2007 05:35
A note as to how the comparisons of case values to the switch value are being made would be useful, since it appears, the comparison operator === is not available, only ==. That means, according to the example given, in

switch($a)
...
case 0
  statement-0
case FALSE
  statment-1
case NULL
  statement-2
...

both statement-1 and statement-2 cannot be executed, no matter what the value of $a is, including FALSE.
Drake
16-Jul-2007 05:51
Also,
when using switch for mode selecting on websites like:

switch($_GET['mode']) {
  case "gallery":
    //code
  break;
  case "news":
    //code
  break;
  case "stuff":
    //code
  break;
  default, etc etc
}

Will NOT trigger the string == 0 bug, because $_GET automatically parse anything passed to them as strings.
(same applies for all browser variables: SESSION, POST etc)

so passing:
mode=0
into the address bar is the same as:
$_GET['mode'] = "0"; //not $_GET['mode'] = 0;

thought it may help.
2mareks (at) gmail (dot) com
29-Nov-2006 11:03
In reply to earlier comment, "switch"- I found this to be one of the best ways to interpret 'actions'. Simply create a new instance of Handler_action before including any content source files. This is a highly stripped version of the class.

The real one I created handles (and secures) input for $_GET and $_POST, creates a 'permission' array that only allows certain actions to be called by non-admins, and creates handy little diagnostic messages that can be displayed upon redirecting.

On that note, the beauty in this class really shines in the simple redirect. You wont be left with ugly URLs like, "http://www.domain.com/path/to/script.php?action=blah&var1=123". Rather, you will be left with something like "http://www.domain.com/path/to/script.php"- helps protect some of the site by not showing any vulnerabilities in URLs.

Also, this class keeps all actions organized neatly by directly passing $_GET vars to the actions through function parameters.

<?php
  class Handler_action {
    function __construct( ){
      //Add code here to secure attacks through $_GET or use $_POST
      $action = $_GET["action"];
  
      //$actions_index conventions:
      //'action_name' => array( 'arg1', 'arg2', 'etc', ['/redirect/to/path' | NULL ] )
      $actions_index = array(
        'create' => array( $_GET['newVar1'], $_GET['newVar2'], '/home.php' ),
        'edit' => array( $_GET['id'], $_GET['otherVar'], '/home.php' ),
        'delete' => array( $_GET['id'], '/other_script.php' )
      );
              
      if( $action && array_key_exists( $action, $actions_index ) ){
        $redirect_path = array_pop( $actions_index[$action] );
        call_user_func_array( array( &$this, $action ), $actions_index[$action] );
        if( $redirect_path )
          header( "Location: http://www.domain.com{$redirect_path}" );
      }
    }

    //being defining actions now
    function create( $new_var1, $new_var2 ){
  
      //code...
  
    }
    function edit( $id, $other_var ){
  
      //code...
  
    }
    function delete( $id ){
  
      //code...
  
    }
  }
?>
27-Oct-2006 10:29
I could have used a swich for this, but I found that using the array was much faster.

    $action = $_GET['action'];

    $pages = array
    (
      'edit'   => './edit.php',
      'search' => './search.php'
    );

    if(strlen($pages[$action]) > 0)
    {
      require $pages[$action];
    }
    else
    {
      require './default.php';
    }
sneskid at hotmail dot com
23-Oct-2006 11:38
In regard to what dohpaz at kennethpaul dot com wrote.

If you ever have time you may want to test out having a premade associative array with the required elements eaqualing the needed value. Then assign the value based on the array element.

in dohpaz's month example it would look like this:
<?php
$arr_month = array(
'January' => 1,
'February' => 2,
'March' => 3,
'April' => 4,
'May' => 5,
'June' => 6,
'July' => 7,
'August' => 8,
'September' => 9,
'October' => 10,
'November' => 11,
'December' => 12);
foreach($arr_month as $k => $v) {$arr_month[substr($k,0,3)] = $v;} // autogen a 3 letter version

//note that the overall size will be 23 because May will only exist once

$month = 'Jan';
$month = $arr_months[$month];
echo $month; // outputs: 1
?>

It beats a switch in this case.

I did some benchmarking.
The array system is faster than the switch system.

Here were my average time results of 1000 itterations of assigning the numeric value to the month.
The value was randomized between each itteration (this was not added to the benchmark value), so each method was simulated with various data to stress different points.

array:
'avg' => 1.09958648682E-6
switch:
'avg' => 4.32157516479E-6
switch (true):
'avg' => 6.90913200378E-6

Contrary to what dohpaz suggested I found that a normal switch was faster than a switch(true) version.

I repeated these test several times to take into acount server load variations. The ratios were always consistent.

The array way is notably faster.
mdirks at gulfstreamcoach dot com
13-Sep-2006 02:09
In response to scott at firefallpro dot com:

"Also note that even though a conditional statement needs to be explicitly set in each case to gain expected behavior, the switch can still execute faster then an "if/elseif/else" block because PHP will not continue to evaluate conditions once a case has been satisfied."

This is not accurate as far as the documentation would say. PHP does not (or at the very least should not) continue to evaluate other "if/elseif/else" statements () on the same level once a true statement is found. It will (should) "short-circuit" to after the else block in the same depth-level, once it has finished executing all the code in it's block naturally.

Where the switch statement wins out over an "if/elseif/else" block is it's ability to "fall though" to instructions in following cases until a break is encountered. Coding something similiar using "if/elseif/else" statements could get really messy and buggy really fast depending on the switch statement.
scott at firefallpro dot com
22-Dec-2005 08:01
It's has already been mentioned indirectly in a few posts, but it is important to realize that switch statements evaluate each case with the "==" operator by default. This can lead to unexpected results when comparing strings to integers, because PHP will convert the string to an integer. In many cases this means a string can be equivalent to the integer 0.

Example:
<?php
$x = 0;

switch($x) {
case "a":
    echo "a";
    break;
case "b":
    echo "b";
    break;
default
    echo "default";
}
?>

The result will be an "a" echoed out. What PHP does in this instance, is once it realizes that it's attempting to compare string ("a") to an integer (0), it converts "a" into an integer which ends up satisfying the first case.

The rules for string conversion into integers is available at:
http://us3.php.net/manual/en/language.types.string.php

The easiest way to combat this issue is to force type comparison by using the "===" operator. This makes PHP forego the string to integer conversion.

Example:
<?php
switch(true) {
case $x === "a":
    echo "a";
    break;
case $x === "b":
    echo "b";
    break;
default
    echo "default";
}
?>

Or the switch input can be type-casted to always be a string, etc.

Also note that even though a conditional statement needs to be explicitly set in each case to gain expected behavior, the switch can still execute faster then an "ifelse" block because PHP will not continue to evaluate conditions once a case has been satisfied.
jonybd at yahoo dot com
28-Jun-2005 12:25
/*
    Have one value need to deal with currency
    - follow as example
*/
while ($row = mysql_fetch_array($v_Result,MYSQL_NUM)) {
    $v_BAL = $row[1]/10000;
    
    switch (TRUE){

        case ($v_BAL <= 0):            //less then 0 , -0
            echo $v_BAL;
        break;
                    
        case ($v_BAL <= 10 AND $v_BAL >= 1):    //less then 10 and greater then 1
            echo $v_BAL;
        break;
                                        
        default:                //default
            echo $v_BAL;
        break;
    }
}
dohpaz at kennethpaul dot com
04-Jun-2005 10:24
[Editor's note: Changed the second switch to make it work as intended.]

I haven't seen anything specifically pointing this out, but you can get a small performance increase in your code by re-structuring your complex switch statements.

For example, I was using the following switch to convert textual month names into their numerical counterparts from the Date header of email on my pop server:

switch ($month_name) {
  case "Jan":
  case "January":
    $month = "1";
    break;
  ...
}

Even just looping through 15 emails on the server, it would take upwards of around 9-10 seconds! So, I decided to shorten my switch statement to something like this:

switch (TRUE) {
  case ($month_name == "Jan" || $month_name == "January"): $month = "1"; break;
  ...
}

Doing this I actually shaved 3 seconds from my script's execution time!!! I thuoght this was well worth noting for other coders out there who are looking to optimize their PHP code.
ant at loadtrax dot com
24-Mar-2005 05:01
This caught me out. The number '6' when compared with the string '6b' returns true. The solution is to either typecast the compare  -  ie, " switch ((string)$type): "  or to make sure $type is a string (eg $type="6")

<?
$type=6;
switch ($type):
    case "6b":
        print "6b: ";
        print $type;
    break;
    case "6":
        print "6: ";
        print $type;
    break;
endswitch;
?>
Bruno Feu
22-Mar-2005 09:22
You can solve the problem by just writing the following piece of code:

<?php
$x = 18;
$y = 6;

switch ($x) {
   case ($y * 4):
   case (9 * 3):
       echo "Member";
       break;
   default:
       echo "Not a member";
}
?>
ach aat bitfabrik doot de
14-Mar-2005 11:21
So instead of writing the code shown below it would have to be like this:

<?php
$x = 18;
$y = 6;

switch ($x) {
   case ((($y * 4) || (9 * 3))?$x:false):
       echo "Member";
       break;
   default:
       echo "Not a member";
}
?>

So now the case expression contains an if statement in simplified notation which either returns the value of $x if the expression is true (so the case matches) or false, if the expression was false (so the case does not match).
Be aware that it only works if $x never actually is "false" because then it would match in either case. So the "false" in the above code should always be any random value which is not a possible value for $x.
gregory dot mccoy at pleasedontspam dot cafeoui dot net
13-Mar-2005 05:07
In the post:
----------------------------------------------------
design at hyperoptix dot com
18-Feb-2004 12:46
Boolean logic does not work inside case statements:

<?php
$x = 18;
$y = 6;

switch ($x) {
   case (($y * 4) || (9 * 3)):
       echo "Member";
       break;
   default:
       echo "Not a member";
}
?>

echoes "Member".
----------------------------------------------------
there were many responses but all seem to miss the point.  You cannot mix apples and oranges.  The "switch($x)" establishes that this "switch" statement will be a Relational syntax while the "case" qualifier uses a Logical syntax.  There must be a match.  Either change "switch($x)" to "switch(true)" or change "case(($y * 4) || (9 * 3)):" to resolve to a value.

The syntax of the original post is like a cop that says, "I want all of your answers to reflect truth.  So, are you eighteen?"  The respondent says, " 4 x 4 or 11 + 5".  Need I say more?
rdoggett at oz dot net
19-Jan-2005 01:01
Here's an often overlooked way of doing (nearly) the same thing:
<?php
echo ($i == 0) ?   "i is zero" :
      (($i == 1) ?  "i equals 1" :
      (($i == 2) ?  "i equals 2" : ""));
?>
This may be an idiomatic surprise at first.  But the clean and concise code speaks for itself.

Beware; PHP seems to parse the ternary operator with a different precedence than other languages such as C or perl or javascript.  This means PHP requires nested parenthesis around each nested group to avoid unexpected results.  Even so, this construct is still very understandable and maintainable, compared to the equivalent switch or if statements.
Derek Ethier
22-Dec-2004 07:43
A word of caution around the order used for the case/default controls.  I notice that a lot of people do not break; the default section and the following could lead to incorrect results when run.

$a = "lowercase";

switch ( $a ) {
  default:
    $a = strtoupper( $a );
    print $a . "<br />";

  case ( 'LOWERCASE' ):
    print $a . "<br />";
    break;
}

Result:
LOWERCASE
LOWERCASE

Placing a break; in the default control will result in:
LOWERCASE

.. as expected.  Also, placing the default section at the bottom (as in an else control) will also display the correct result.
Bachsau
20-Dec-2004 12:52
Be carefull: If you want to test the return of a function, you have to use switch, because if you use 'if' and 'ifelse', your function will be executed every time again.

For example if use use the following construct:

if(file_get_contents('file.htm', 0) == 'typ1') {
     // Do one thing
}
ifelse(file_get_contents('file.htm', 0) == 'typ2') {
     // Do the second thing
}
ifelse(file_get_contents('file.htm', 0) == 'typ3') {
     // Do the third thing
}

The file will be requested 3 times!!!

If you use the following:

switch (file_get_contents('file.htm', 0)) {
     case 'typ1': // Do one thing
     break;
     case 'typ2': // Do the second thing
     break;
     case 'typ3': // Do the third thing
}

The file will be requested only once!!!
pentek_imre at mailbox dot hu
23-Oct-2004 11:25
Using select is like using == (instead of ===) in an if statement. Let's see an example:
<?php
function Describe($Q)
{
  var_dump($Q);
  echo ": ";
  switch ($Q)
   {
    case "0":
     echo "String zero";
     break;
    case 0:
     echo "Integer zero";
     break;
    case NULL:
     echo "NULL NULL";
     break;
    case FALSE:
     echo "Boolean FALSE";
     break;
    case "":
     echo "Empty string";
     break;
    default:
     echo "Any other value";
     break;
   }
  echo "<BR>\n";
}
Describe("0");
Describe(0);
Describe(NULL);
Describe(FALSE);
Describe("");
Describe(1);
?>
Output (PHP 5.0.1) is:
string(1) "0" : String zero
int(0) : String zero
NULL : Integer zero
bool(false) : String zero
string(0) "" : Integer zero
int(1) : Any other value
tom AT csbanana DOT com
12-Oct-2004 05:18
If you're using switch() inside a function and you're returning a $var inside each case, you won't need to include break() as return() will end the execution of the switch and function.
manicdepressive at mindless dot com
21-Apr-2004 11:43
Be careful if distinguishing between NULL and (int)0.  As implied in the above documentation, the case statements are equivalent to the '==' operator, not the '===' operator, so the following code did not work as i expected:

<?php
$mixed = 0;
switch($mixed){
   case NULL: echo "NULL";  break;
   case 0: echo "zero";  break;
   default: echo "other"; break;
}
?>

Instead, I may use a chain of else-ifs.  (On this page, kriek at jonkreik dot com states that "in most cases [a switch statement] is 15% faster [than an else-if chain]" but jemore at m6net dotdot fr claims that when using ===, if/elseif/elseif can be 2 times faster than a switch().)

Alternatively, if i prefer the appearance of the switch() statement I may use a trick like the one nospam at please dot com presents:

<?php
$mixed = 0;
switch(TRUE){
   case (NULL===$mixed): //blah break;
   case (0   ===$mixed): //etc. break;
}
?>

code till dawn! mark meves!
ezekiel at superquenelles dot com
25-Mar-2004 01:40
In reply to Alex Fung :
The following code doesn't work :

<?php
$x = 18;
$y = 6;

switch ($x) {
   case (($y * 4) || (9 * 3)):
       echo "Member";
       break;
   default:
       echo "Not a member";
}
?>

Why :
<design at hyperoptix dot com> want to test if $x == $y*4 or $x == 9*3 ($x == (($y*4)||(9*3))
However the case statement evaluate the value of (($y*4)||(9*3)) that is always true because 9*3=27 (!=0)
That's why this code always return true when $x != 0.
The correct code would be :

<?php
$x = 18;
$y = 6;

switch ($x) {
   case (($y * 4)):
   case ((9*3)):
        echo "Member";
        break;
   default:
       echo "Not a member";
}
?>

Boolean logic work inside case statement, you just need to know that the expression in the case statement is first evaluated then compared with the evaluated value in the switch statement.
php dot net dot 1 at yogelements dot com
20-Jan-2004 06:39
Declaring a variable (actually an array) as static w/in a switch{} spun my wool for a while:
don't:
<?
function ss() {
    switch ("bug") {
        case "bug" :
           static $test = "xyz";
           break;
        default :
           static $test = "abc";
    }
echo $test;
}
ss(); //abc
?>
do:
<?
function tt() {
    static $test;
    switch ("fix") {
        case "fix" :
           $test = "xyz";
           break;
        default :
           $test = "abc";
    }
echo $test;
}
tt(); // xyz
?>
gmgiles at pacbell dot net
19-Jan-2004 09:07
Did you know that switch() and case() can also accomodate things like basic math calculations and counter incrementing? They do. In this example, I use a switch statement (which is inside of a while loop) to alternate the background color of a table row. It gives me a cool spool-printer-paper effect.

<?php
$rows_per_color = 5;  // change bgcolor every 5 rows
switch($ctr++) {
    case 0:
        $bgcolor = "#ffffff";
        break;
    case ($rows_per_color):
        $bgcolor = "#ff0000";
        break;                
    case ($rows_per_color * 2):
        $bgcolor = "#ffffff";
        $ctr = 1;
        break;        
}
?>

As you can see, I increment $ctr by 1 in the switch() itself, and the final case() does a simple calculation. Simple, but powerful. [Remember, the above example is inside of a while() loop... each time it iterates, switch increments $ctr.]
phpmanual at nos-pam dot sadlittleboy dot com
10-Jan-2004 01:32
Regarding bishop's comment below, although using:
   switch($bug === 0 ? '' : $bug) {
may work, ( and although I do like the ternary operator, :) it might be more intuitive/readable to use this instead:
   switch( (string)$bug ) {
which typecasts the variable to a string to ensure that "0" will be handled correctly.
jon
08-Dec-2003 08:48
In response to the entry by "kriek at jonkriek dot com", I think you would probably be better of doing this:
<?php
    // ensure $_GET['go'] is set, an integer, and not 0
    // then, set nav number; default to 1
    $nav = ( isset($_GET['go']) && (intval($_GET['go']) == $_GET['go']) && $_GET['go'] ) ?
        intval($_GET['go']) : 1;

    // format navigation string and include
    include(sprintf("Page%02d.php",$nav));    
?>

... as oppposed to the switch setup you recommended, which is limited to the number of cases you specify...
havar at henriksen dot nu
14-Sep-2003 08:54
Remember, that you also could use functions in a switch.
For example, if you need to use regular expressions in a switch:

<?php
$browserName = 'mozilla';
switch ($browserName) {
  case 'opera':
    echo 'opera';
  break;
  case (preg_match("/Mozilla( Firebird)?|phoenix/i", $browserName)?$browserName:!$browserName):
    echo "Mozilla or Mozilla Firebird";
  break;
  case 'konqueror':
    echo 'Konqueror';
  break;
  default:
    echo 'Default';
  break;
}
?>

or you could just use a regular expression for everything:

<?php
$uri = 'http://www.example.com';
switch (true) {
  case preg_match("/$http(s)?/i", $uri, $matches):
    echo $uri . ' is an http/https uri...';
  break;
  case preg_match("/$ftp(s)?/i", $uri, $matches):
    echo $uri . ' is an ftp/ftps uri...';
  break;
  default:
    echo 'default';
  break;
}
?>
(cgibbard) student math uwaterloo ca
10-Aug-2003 09:30
Just in reply to the comment about 2 digit numbers: something octal certainly is going on. Integer literals prefixed with a "0", like in C and several other languages, are treated as octal. Similarly, integer literals prefixed with "0x" are treated as hexadecimal. Seeing as this is the case, 08 and 09 are not valid integer literals. It turns out that php treats them as 0 (it would probably be better to fail with an error message, but it doesn't). Bottom line? Don't prefix numbers with 0 in code unless you mean octal. Format them as you print them with printf, like so: printf("%02u", $my_unsigned_int); or if you will, use sprintf to get a string representation rather than printing on stdout.
bishop
14-Jul-2003 07:26
As jason at devnetwork dot net and others have pointed out, using switch() when you wish to compare against strings can be dangerous:

<?php
$bug = 0;
switch ($bug) {
    case 'fly':
        echo 'flies buzz';
        break;

    case 'mantis':
        echo 'mantes pray';
        break;

    default:
        echo 'swat, splat, you are dead';
        break;
}
?>

Will print "flies buzz", NOT "swat, splat, you are dead".
Remember PHP says that 'fly' == 0, or in general string == 0 is true.

Anyway, avoid that with:

<?php
$bug = 0;
switch ($bug === 0 ? '' : $bug) {
    case 'fly':
        echo 'flies buzz';
        break;

    case 'mantis':
        echo 'mantes pray';
        break;

    default:
        echo 'swat, splat, you are dead';
        break;
}
?>

Prints out what you expect:

Swat
Splat
You are dead

P.S.: that's an empty string (single quote single quote), not a spurious double quote.
shawn at evilest dot net
04-May-2003 09:50
You can also nest switch statements inside case statements:

<?php
   // Set argument handlers
    $argv = explode(",", urldecode(getenv('QUERY_STRING')));
    $argc = array_shift($argv);
    $argd = array_shift($argv);
    $arge = array_shift($argv);
?>

   // Begin switching

<?php
    switch ($argc) {
        case 'home': {
             print('This is $argc, home case.');
            break;
        }
        case 'subsection': {
                switch ($argd) {
                     case 'links': {
                            switch($arge) {
                                case 'display': {
                                print('This is $arge, subsection,links,display case.');
                                break;
                                }
                           }
                    }
                }
        }
    }
?>
i luv spam
25-Apr-2003 06:46
Noticed some odd switch behavior worth mentioning:

Switching on a variable set as $var="08" and forgetting the quotes within the case results in different behavior depending on the two digit number the variable is set to.

For "01" to "07", using a case like
  case 01:
the case is triggered.

For "08" or "09" the case is skipped.

For "10" to "12" the case is triggered.

Looks like something octal may be going on.

Anyway, not a problem once the case is changed to:
  case "08":
as it should have been from the start.  Just odd.
jason at devnetwork dot net
24-Mar-2003 10:51
It should be pointed out that this:

<?php

$var = 0;

switch ( $var )
{
    case "something":
        $foo = "Broken";
        break;
    default:
        $foo = "Okay";
        break;
}

echo $foo;

?>

Will print out "Broken".  It's not broken, because in PHP, when an Integer and a String are compared, the string is == 0.  So 0 == "something".  However, this is not apparent.  switch() does not do type checking.
kriek at jonkriek dot com
05-Mar-2003 02:13
Nice, clean, template style navigation. In most cases it is fifteen percent faster to use switch/case/break instead of if/elseif/else. Of course this depends on your application and individual code results do very.

<?php
    switch ($_GET['go']) {
        case "1": $inc = 'Page01.php';
        break;
        case "2": $inc = 'Page02.php';
        break;
        case "3": $inc = 'Page03.php';
        break;
        case "4": $inc = 'Page04.php';
        break;
        default: $inc = 'Page01.php';
        break;
    }
    include ($inc);
?>
jemore at m6net dotdot fr
14-Feb-2003 12:26
siwtch() made always a type conversion before comparing all the case value (PHP4.3.0), so the following statement
<?php
      // $a = 'abc0' or 'abc1' or 'abc2', so this is a string
      switch ($a)
      {
        case 'abc0' : $nb += 1; break;
        case 'abc1' : $nb += 2; break;
        case 'abc2' : $nb += 3; break;
      }
?>
is slower than the following statement
<?
      if ($a === 'abc0') $nb += 1;
      elseif ($a === 'abc1') $nb += 2;
      elseif ($a === 'abc2') $nb += 3;
?>
because the '===' (3 equals signs) compare value without type conversion. Using a if/elseif/elseif structure instead of switch/case/case can be 2 times faster (I have made a test)
rmunn at pobox dot com
23-Jan-2003 07:21
In answer to njones at fredesign dot com, what you're seeing is the way the switch statement is supposed to work. The switch statement evaluates the cases, top to bottom, until it finds the first one that matches the value being switch()ed on. So, for example, if you had:

<?php
switch(2) {
case 1: echo "One\n"; break;
case 2: echo "Two\n"; break;
case 3: echo "Three\n"; break;
case 2: echo "Two again\n"; break;
}
?>

Only "Two" would get echoed. "Two again" would NOT get echoed, because once the first case matches, the rest of them do NOT get evaluated. As soon as a matching case is found, the statements starting at that case get executed until the first break, then control flows out the bottom of the switch block.
theonly dot mcseven at gmx dot net
19-Jan-2003 04:44
working a bit around with it I found out that it is not possible to
compare the variable with two different values in one step like this
(system running a w2k server, apache2.0.43 & php430):

<?php
switch ($myvar) {
case ("foo" || "bar"): //do something
break;
case ("other"): //do another thing
break;
default:
}
?>

rather use:

<?php
switch ($myvar) {
case ("foo"):
case ("bar"): //do something
break;
case ("other"): //do another thing
break;
default:
}
?>
turk162 at ALLSPAM_hotmail dot com
03-Dec-2002 12:55
On PHP V4.2.1, running on IIS5 as a CGI, I found an anomaly with how SWITCH handled strings.  I've heard that this problem doesn't exist on V4.2.3 on Apache.

This snippet took the wrong branch:

<?PHP
$wonum = '20010E0';
SWITCH ($wonum):
   CASE '20010D0';
      ECHO "<BR>Branching at D with wonum: " . $wonum;
      BREAK;
   CASE '20010E0';
      ECHO "<BR>Branching at E with wonum: " . $wonum;
      BREAK;
ENDSWITCH;
?>

Type casting with $wonum = (STRING) '20010E0'; didn't help.
Changing the order of the CASEs made no difference (it shouldn't, but...)

What did work was using MD5 to force a string comparison:

<?PHP
$wonum = MD5('20010E0');
SWITCH ($wonum):
   CASE MD5('20010D0');
      ECHO "<BR>Branching at D with wonum: " . $wonum;
      BREAK;
   CASE MD5('20010E0');
      ECHO "<BR>Branching at E with wonum: " . $wonum;
      BREAK;
ENDSWITCH;
?>

Moral: test test test
hexa at h3xa dot com
14-Nov-2002 03:55
this is a simple function that returns a random string and uses
switch to determine what kind of string you want.
function r( [string prefix][,int type,][,int chars] );

type = 1 -> only numbers
type = 2 -> only letters
type = 3 -> both

<?php
function r($prefixo = "",$tipo = 3,$numcaracteres = 10) {
    switch ($tipo) {
        case 1:
            for ($x = 1; $x <= $numcaracteres; $x++) {
                $rnum .= chr(rand(48,57));
            }
            return $prefixo . $rnum;
            break;
        case 2:
            for ($x = 1; $x <= $numcaracteres; $x++) {
                if (rand(1,2) == 1) { $rletras .= chr(rand(65,90)); }
                else { $rletras .= chr(rand(97,122)); }
            }
            return $prefixo . $rletras;
            break;
        case 3:
            for ($x = 1; $x <= $numcaracteres; $x++) {
                $r = rand(1,3);
                if ($r == 1) { $rstring .= chr(rand(65,90)); }
                elseif ($r == 2) { $rstring .= chr(rand(97,122)); }
                else { $rstring .= chr(rand(48,57)); }
            }
            return $prefixo . $rstring;
            break;
    }
}
?>
chernyshevsky at hotmail dot com
28-May-2002 10:45
Be very careful when you're using text strings as cases. If the variable supplied to switch() is an integer, the cases would be converted to integer before the comparison is made (usually to zero). The following snippet prints "hello".

<?php
$a = 0;
switch($a) {
case 'Hello': echo "Hello";
break;
}
?>
paz at spiralon dot com
16-May-2002 02:44
In case : ) it helps someone, I was able to clean up some hairball code by using nested switches (didn't see it mentioned here).  Thanks to all those who are writing examples - I love this site!

<?php
$string_match="second";
switch ($string_match) {
case "first":
case "second":
case "third":
    print "<H3>Something for all three</H3><br>";
    switch ($string_match) {
      case "first":
      print "something for first only";
      break;
      case "second":
      case "third":
      print "something for the other two";
      break;
    }
break;
default:
print "<H3>no match</H3>";
}
?>
gray dot quinn at catch-e dot com dot au
22-Mar-2002 06:00
To get the conditional statement to work for the above example use this:

<?php
$chr = substr($a,$i,1);
switch (TRUE) {

case $chr == "�" || $chr == "�" || $chr == "�" || $chr == "�":
$a = str_replace(substr($a,$i,1),"a",$a);
break;

case $chr == "�" || $chr == "�" || $chr == "�":
$a = str_replace(substr($a,$i,1),"e",$a);
break;
?>

}
PeterC at (spamme)rm-rfsplat dot com
06-Feb-2002 08:55
Along the lines of using expressions in switch statements.  I came across some code which wrapped switch statements in 'if' blocks like so...
if (isset($var) {
    switch($var) {
        ....
    

But I found the following a little cleaner.

switch ( isset($var) ? $var : defaultValue ) {
...
x@x
25-Jul-2001 09:29
often you will have to perform multiple actions in sequence, but this sequence must be broken once one of them detects a stop condition (such as an error, when validating form request variables).
One way is to use:

if (check1()
&& check2()
&& check3()
) valid();
else error();

But when the sequence is long and must reordered, this can be errorprone because not all line of check have the same syntax (imagine that you want to comment one of them).

Another way is to rewrite it as:

check1() and
check2() and
check3() and
...
valid() or
error();

The above syntax does not fit well when the valid() code must be several statements.
An alternative syntax can be:

switch (false) {
case check1():
case check2():
case check3():
  error();
  break;
default:
  valid();
}

This last equivalent sample show you that each case expressions is evaluated, until one of them evaluates to a value equal (==) to the switch expression. Above, the error() code will only be called if one of the check evaluates to false. And the valid() code will only be evaluated only if the switch reach the default, which means that none of the above check returned false...
bensphpnetemail at supernaut dot org
29-Jun-2001 06:14
It's obvious, but might still bear explicit mention that you can conditionally execute the BREAK statement in order to allow a CASE to fall through to the next CASE.  

e.g.:-> Here, CASE 1 will fall through and the DEFAULT CASE statements will also be executed unless $somevar is true.

<?php
switch ($i) {
    case 0:
        print "i equals 0";
        break;
    case 1:
        print "i equals 1";
        if ($somevar) {
             break;
             }
    default;
       echo 'Some Default Statements';
        break;
}
?>

Cheers,
Ben Nardone
rtreat2 at tampabay dot rr dot com
30-Mar-2001 11:34
just a further example of the above note, you can do the following type of searching:

<?php
switch (true){
    case ( ereg ("stats",$userfile_name) ):
        echo "processing stats";
        process_stats();
        break;  
    case ( ereg("prices",$userfile_name) ):
        echo "processing prices";
        process_prices();
        break;  
    default:
        echo = "File not recognized!!.";
}
?>

this script could be used to determine data formats for uploaded files based on a nameing conve
ntion. just one example.
nospam at please dot com
15-Nov-2000 01:18
Just a trick I have picked up:

If you need to evaluate several variables to find the first one with an actual value, TRUE for instance. You can do it this was.

There is probably a better way but it has worked out well for me.

switch (true) {

  case (X != 1):

  case (Y != 1):

  default:
}
추천추천 : 1279 추천 목록
번호 제목
3,020
 [HTML5] <video> - DOM으로 제어하기
3,019
 HTML5 video 태그에서 영상 좌우반전
3,018
 PHP - 특정 태그 및 문자열 추출, 제거
3,017
 [PHP] define과 defined의 차이
3,016
 우클릭 완벽차단 스크립트
3,015
 iframe 높이 100% 맞추기
3,014
 curl 함수를 이용한 HTTP REFERER 변경
3,013
 윈도우10 시스템 파일 및 Dism 검사
3,012
 텍스트 줄바꿈, 글자자르기 CSS
3,011
 jQuery Mobile에서 유용한 코드 10가지.
3,010
 [PHP] dirname()함수와 $_SERVER 관련 상수들
3,009
 [PHP] 파일 크기, 사이즈 불러오는 함수, filesize()
3,008
 [jQuery] jQuery Quick API
3,007
 [ transition ] 링크 hover 색상 변화 속도 조절
3,006
 PHP 5.3.0 에서 사라진 함수들 대체
3,005
 어떤 파일들이 include 나 require 되었는지 확인하는 함수(get_included_files)
3,004
 PHP 날짜 형식 및 계산하기(날짜 더하고 빼기)
3,003
 jQuery Mobile에서 유용한 코드 10가지.
3,002
 값이 배열 안에 존재하는지 확인하는 in_array함수
3,001
 사용자가 웹브라우저에서 뒤로가기를 했을때 감지하는 방법
3,000
 [jQuery]버튼 활성화, 비활성화
2,999
 jQuery show() / hide() / toggle() 사용법
2,998
 jquery 여러가지 이벤트
2,997
 border-radius 속성
2,996
 네이버 오픈API 음성합성 API 사용하는 PHP 샘플코드
2,995
 UTF8 한글 자르기..
2,994
 iconv 에러 발생시 계속 처리하기 옵션
2,993
 [PHP] 현재 페이지의 도메인 , URL 정보 알아내기.
2,992
 [PHP] 막강 기능 배열..
2,991
 [CSS] - Input clear `X ` 버튼 제거 ( IE, Chrome, Firefox )
2,990
 [Mobile] - 모바일웹 Href 태그속성들
2,989
 [JqueryMobile] - 현재화면의 가로세로 사이즈 구하기
2,988
 [JqueryMobile] - 화면의 가로, 세로 사이즈 구하는 방법
2,987
 jquery로 가로 넓이(width), 세로 높이(height) 자동 조절
2,986
 iframe 높이 jquery로 자동조절하기
목록
추억의가요방(가사포함)
인기절정뽕짝파티
인기트로트모음
지루박디스코메들리
밤무대애창곡
전자올겐경음악
세월따라노래따라
가슴시린트로트
트로트쌍쌍파티
7080추억속으로
종합성인가요방
못잊을옛날노래
카바레 음악
트롯디스코팡팡
관광 메들리
트롯카페
가요감상실
추억의옛노래
스페셜가요광장
BillBoard Free
추천가요모음
경음악.전자올겐
스페셜음악여행
WOLRD POPs
K-POP\BillBoard
POP TOP BEST

최신인기가요특집
추천가요\인기
F뮤직 인기\발라드
F뮤직 애창\트로트
트로트성인가요
인기가요
프리미엄 POP
경음악\기타
프리미엄 최신가요
프리미엄 성인가요
가요축제\트롯1번지
댄스\메들리\리믹스
카페\명상\경음악\기타
뮤직트로트 부산광역시부산진구 가야동 ㅣ 개인정보취급방침
Copyright (C) musictrot All rights reserved.