Thursday, January 13, 2011

Switch case Statement

hye guys!now for this post i will show to u on how to use Switch case in PHP.Below is my explanation + my example:


ok,the concept of switch..case statement is quite similar to if...else statement.but,to me,this switch...case concept is quite convenient because u will state what variable do u want to check.so,that's why the code is begin with like this:

 switch ($AnyVariableDoYouLike) {

and for case:For exmple:

case 'conditionOfVariable':     quite similar to    if($AnyVariableDoYouLike==1)

as u can see that,case is quite similar to if statement but if statement use comparator operand to check the condition as we learn before this.while default statement is just same with else statement.For exmple


default:                                                       else

 print('no food');      is same with            print('no food');               

But,the most significant one is switch..case statement has break for each case n default

Here the code,try it,test it feel it by urself!


<?php

$food ='delicious';

switch ($food) {
 

case 'cheap':
print(
'the variable is $food and it is value is cheap');
break;


case 'delicious':
print('
the variable is $food and it is value is delicious' );
break; 


default:

 print('no food')
break ;

}
?>
 
N/NOTE:so far okey?well that all the best that i can present to u,huhu

No comments:

Post a Comment