Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Information website that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,111 questions

16,499 answers

573 users

How to use switch case statement in PHP

Online Web Development & Programming Courses | Udemy
28 views
asked Jun 3, 2016 by avibootz
edited Dec 29, 2016 by avibootz

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
...