Thursday 27 February 2014

Switch Statement in php

php control structures
Switch Statement in PHP Programming Languages. This tutorial is for Switch Statement. Now you may already observed in my earlier tutorials of if_else statement and if_else if statement for multiple checks, but what if you have a lot of numbers or strings to check or something you need to check like long list. Switch Statement is a better option . Its better, cleaner and faster way of using if _else and else if statements.

Code for switch statement in php (Screenshot1):-

Code For Switch Statement In php
Code For Switch Statement In php Screenshot1

<?php

$number = 1;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

}

?>



Output (Screenshot2):-

Switch Statement
Screenshot2
In Switch Statement structure we have “switch” as a keyword where we refer declared value of variables in switch. Inside the switch blog we can evaluate multiple operations in different cases. Inside case’s along with operations we can call/declare functions, echo any statement ,and the very important thing to notice is to write “break” keyword at the time of ending any case. Eg. in below example we have a variable $number that is initialized as 2 and in switch keyword we have given reference to that variable to evaluate it further with case.In this program we had convert integer data to string. So, instead of writing number equal to 1 echo “one”, else if number equal to 2 echo “two”, else if number equal to 3 echo “three”, but this will confuse programmer in large programs if used!Switch Statement makes it easy by taking
case 1: echo one
case 2: echo two
case 3 echo three
and basically doing this evaluates our number too.

Code for Switch Statement in php (Screenshot3):-

Switch case
Code For Switch Statement in php Screenshot3

<?php

$number = 2;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

}

?>

Output (Screenshot4):-

Switch statement in php
Screenshot4
In this example variable is 2 so its true for case 2 which will echo “two”.

Code for Switch Statement in php (Screenshot5):-


switch statement php
Screenshot5


<?php

$number = 2;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

}

?>

Output (Screenshot6):-

php switch case
Screenshot6
In this example variable is 3 so its true for case 3 which will echo “three”.

Code for Switch Statement in php (Screenshot7):-

Code for switch statement
Screenshot7

<?php

$number = 4;

switch ($number) {

 case 1:

 echo 'One';

 break;

case 2:

 echo 'Two';

 break;

case 3:

 echo 'Three';

 break;

default:

 echo 'Number not found';

 break;

}

?>

Output (Screenshot8):-

Switch case php
Screenshot8
In this example what if number does not evaluate any case, for handling this we have default case. In this example case 4 does not exit. So,via default case we will echo out “Number not Found”.

Code for switch statement (Screenshot9):-

switch case php
Screenshot9

<?php

$day = 'Monday';
switch ($day) {

 case 'Saturday':

 case 'Sunday':

 echo 'It\'s a weekend.';

 break;

default:

 echo 'Not a weekend.';

 break;

}

?>

Output (Screenshot10):-

switch case php
Screenshot10
In this example I am taking variable string and evaluate it with string to echo out whether it is “weekend” or “Not a weekend”. For Monday it’s “Not a weekend”.

Code for switch statement (Screensho11):-

switch case
Code for switch case Screenshot11


<?php

$day = 'Saturday';
switch ($day) {

 case 'Saturday':

 case 'Sunday':

 echo 'It\'s a weekend.';

 break;

default:

 echo 'Not a weekend.';

 break;

}

?>

Output (Screenshot12):-

Output Screenshot12
In this example I am taking variable string and evaluate it with string to echo out whether it is “weekend” or “Not a weekend”. For Saturday ”It’s a weekend”.
You can try for Sunday as well by changing variable to Sunday and thus it evaluates case for Sunday. So,
in thus in Switch case you can compare multiple cases and always use default for handling variables not evaluating or comparing cases.


No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates