Sunday 9 June 2013

SWITCH STATEMENT IN PHP

A switch case statement is a nothing but a selection statement, it can be thought of as a situation when you want to compare a single variable with multiple values, and for each value you have different code for each such value. The switch case statement in php is exactly similar to that of the C and C++, except one thing that in C and C++ you can't have real numbers for the comparison, whereas in php you can have comparison of real numbers.

Source Code illustrating use of switch case statement in php:-
/********************************************************************************/
<html>
                <head>
                                <title>constants</title>
                </head>
                <body>
                                <?php
                                                $a=3.3;
                                                switch($a)
                                                {
                                                                case 1:
                                                                                echo"a is equal to 1";
                                                                                break;
                                                                               
                                                                case 2:
                                                                                echo"a is equal to 2";
                                                                                break;
                                                               
                                                                case 3:
                                                                                echo"a is equal to 3";
                                                                                break;
                                                                               
                                                                case 4:
                                                                                echo"a is equal to 4";
                                                                                break;
                                                                               
                                                                default :
                                                                                echo"a is something else";
                                                }
                                ?>
                </body>

</html>
/*******************************************************************************/
The statements in the default clause are executed only when none of the value in the case is matched.

No comments:

Post a Comment