Switch case in PHP is an alternative to if else command. Switch case makes your code look more beautiful. The function of the code is actually to test a variable against a series of values until it finds a match, and it then executes the code corresponding to that match. Its syntax is : switch (condition) { case labelA: code to be executed if condition = commandA; break; case labelB: code to be executed if condition = commandB; break; default: code to be executed if condition is different from both commandA and commandB; } Code (markup): It takes the condition mentioned in brackets and if the value of the condition = commandA then it will execute the code listed below case commandA and so on. If the value of condition is neither commandA or commandB then we use the default parameter. Let’s take one example : $dp= "digitalpoint"; switch($dp) { case "facebook": echo "The value of \$dp is not equal to facebook "; break; case "orkut": echo "The value of \$dp is not equal to orkut "; break; case " digitalpoint ": echo "You are right value of \$dp is digitalpoint "; break; default: echo "You did’nt guess it right"; break; } PHP: In this the value of $dp is set to digitalpoint and when the case is digitalpoint we get the result listed. Try changing the value of $dp (leaving facebook,orkut and digitalpoint) and when you execute it you get You did’nt guess it right result. Making it more simpler try : $dp="digitalpoint"; switch ($dp) { case "orkut": case "facebook": case "google": echo "The value of \$dp is not orkut nor facebook neither google"; break; case "digitalpoint";: echo "The value of \$dp is digitalpoint and you are right."; break; default: echo "You are wrong "; } PHP: This code takes $dp as the condition and checks if the value of $dp is equal to orkut,facebook or google it will display the same message. If the value of $dp is digitalpoint then it displays the message listed below the case. And then comes the default command which checks that if the value of $dp is not mentioned then the code after default will be displayed. Feedback appreciated Thanks, uworks
Reader may mistakenly assume that break is required part of the switch/case syntax. That's why it's strongly recommended read official documentations instead of 'simpler versions'