Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use switch case statement in PHP

8 Answers

0 votes
$n = 1;
switch ($n) 
{
    case 0:
        echo "n = 0";
        break;
    case 1:
        echo "n = 1";
        break;
    case 2:
        echo "n = 2";
        break;
}

 
/*
run: 

n = 1 

*/

 



answered Jun 3, 2016 by avibootz
0 votes
$s = "bbb";
switch ($s) 
{
    case "aaa":
        echo "s = 'aaa'";
        break;
    case "bbb":
        echo "s = 'bbb'";
        break;
    case "ccc":
        echo "s = 'ccc'";
        break;
}

 
/*
run: 

s = 'bbb'  

*/

 



answered Jun 3, 2016 by avibootz
0 votes
$n = 1;
switch ($n) 
{
    case 0:
        echo "n = 0 <br />";
    case 1:
        echo "n = 1 <br />";
    case 2:
        echo "n = 2 <br />";
}

 
/*
run: 

n = 1
n = 2 

*/

 



answered Jun 4, 2016 by avibootz
0 votes
$n = 1;
switch ($n) 
{
    case 0:
    case 1:
    case 2:
        echo "n < 3";
        break;
    case 3:
        echo "n = 3";
}

 
/*
run: 

n < 3  

*/

 



answered Jun 4, 2016 by avibootz
0 votes
$n = 100;
switch ($n) 
{
    case 0:
        echo "n = 0";
        break;
    case 1:
        echo "n = 1";
        break;
    case 2:
        echo "n = 2";
        break;
    default:
       echo "n is not equal to 0, 1 or 2";
}

 
/*
run: 

n is not equal to 0, 1 or 2   

*/

 



answered Jun 4, 2016 by avibootz
0 votes
$n = 100;
switch ($n):
    case 0:
        echo "n = 0";
        break;
    case 1:
        echo "n = 1";
        break;
    case 2:
        echo "n = 2";
        break;
    default:
       echo "n is not equal to 0, 1 or 2";
endswitch;

 
/*
run: 

n is not equal to 0, 1 or 2   

*/

 



answered Jun 4, 2016 by avibootz
0 votes
$s = "abc";
switch($s)
{
    case 'abc';
    case 'def';
    case 'ghijk';
        echo 'abc - def - ghijk';
        break;
    default;
        echo 'default';
    break;
}

 
/*
run: 

abc - def - ghijk  

*/

 



answered Jun 4, 2016 by avibootz
0 votes
$n = 0;
switch(++$n) 
{
    case 3: echo 3; break;
    case 2: echo 2; break;
    case 1: echo 1; break;
    default: echo "default"; break;
}

 
/*
run: 

1 

*/

 



answered Jun 4, 2016 by avibootz

Related questions

1 answer 125 views
1 answer 124 views
4 answers 222 views
2 answers 188 views
1 answer 178 views
...