Total members 11890 |It is currently Thu Apr 25, 2024 6:46 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





In switch statement , you can check for many conditions at once .and this is depending on the variable give to the switch .you use switch instead of writing condition in form of if/elseif/elseif/elseif/elseif ... in the next part you will find an example for using switch .


PHP Switch Statement Example


Code:
<?php $number = 1;
echo
"my number is $number<br />";
switch (
$number){
    case
1:
        echo
"your team is  x";
        break;
    case
2:
        echo
"your team is  y";
        break;    
    case
3:
        echo
"your team is  z";
        break;    
    case
4:
        echo
"your team is  e";
        break;
    case
5:
        echo
"your team is  r";
        break;    
}


what you think the output is ? :confused:
the output is
Code:
my number is 1
your team is  x


So, the switch operating work on the $number and search for the case when have same value of $number then execute the segments cross ponding to the case. the break; keyword prevent the other case from being executed .If u didn't write it ,all other cases after the case of correct condition will be executed .

The default case :

It is the case that will be executed when no other cases before it is achieved.

example on default case :


Code:
<?php $number = 132;
echo
"my number is $number<br />";
switch (
$number){
    case
1:
        echo
"your team is  x";
        break;
    case
2:
        echo
"your team is  y";
        break;    
    case
3:
        echo
"your team is  z";
        break;    
    case
4:
        echo
"your team is  e";
        break;
    case
5:
        echo
"your team is  r";
        break;    
      default:
        echo
"your number is not recorded !";
        break;
}


what you think is the output of this code ?

The output is :
Code:
my number is 132
your number is not recorded !




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : php switch
 Switch vs Hub     -  
 Use Switch with strings     -  
 network switch     -  
 JavaScript switch example     -  
 java switch example     -  
 switch keyword in c++ usage     -  
 switch command for string value     -  
 restrictions on the values of each case of a switch     -  
 Java Matrices using a Switch Statement     -  
 Hub-Switch-Router-Proxy-Firewall ?     -  



Topic Tags

PHP Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com