PHP SWITCH CASE


php tutorial

PHP SWITCH CASE

php tutorial

Switch case is an alternate reference of if-else in control structure. In many times you need to compare same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to.

Syntax

				
	Switch (variable/expression)
	{
		Case value:
		
			Statement;
			
		Break;
		
		Case value:
		
			Statement;
			
		Break;
		
		Default:
		
			Statement;
	}

						
						
				

Switch Case attributes detail:


AttributesDefinition
switchIt is used to maintain cases and default statement. It execute according to the value that switch hold.
caseIt used to define many statements behalf of different case value and execute after matching switch value to the case value.
breakIt is used to break case statements to other cases that define in switch-case
defaultThis default statement execute when other case value does not match with switch provide value. Behave just like else in if-else

PHP switch case example implement in same php page.

myChoice.php

<html>
<head><title> Switch Case Example - phptutorialonline.com </title></head>
<body>
<form method="post" action="myChoice.php">
My Choice:<select name="choice">
<option>select</option>
<option value="0">Mango</option>
<option value="1">Orange</option>
<option value="2">Banana</option>
 </select>
 <input type="submit" name="submit" value="Accept"/>
 </form>
 </body>
 </html>
 
 
<?php

if(isset($_POST['submit']))
{
	$choice=$_POST['choice'];
	switch($choice)
	{
		case 0:
		echo "<h1> You Choosed mango!</h1>";
		break;
		case 1:
		echo "<h1>You Choosed Orange!</h1>";
		break;
		casecase
		echo "<h1>You Choosed Banana!</h1>";
		break;
		default:
			echo "Wrong Choice try another time!!";
	}
	
}
?>
	

Here Download PHP Switch Case Example