Decision Making Statements

There are various types of conditional statements available in PHP –

  1. if statement
  2. if-else statement
  3. if-elseif-else statement
  4. switch statement

 The switch Statement

Switch statement proves to be a very useful decision-making statement if we have multiple expressions to be evaluated. It is much simpler and quicker to write codes using switch statement rather than a long set of if-elseif-else statements.

The syntax for the switch statement is a bit different from the others –

switch (n)
{
case tag1:
code to be executed if n=tag1;
break;
case tag2:
code to be executed if n=tag2;
break;
case tag3:
code to be executed if n=tag3;
break;
...
default:
code to be executed if n is different from all tags;
}

Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
   
    $answer = 0;
    $flag=false;
	$status="";
    if(isset($_GET["answer"]))
	{
    		$answer = $_GET["answer"];
			$flag=true;
			switch($answer)
			{
		
				 case 1: 
		 			 $status= " Correct";
					 break;
		 		 case 2: 
 		 			 $status= " Incorrect";
					 break;
		  		 case 3: 
 		 			 $status=" Incorrect";
					 break;
			}
  }
	
?>


<?php
echo  "<center>";
echo   "Name of currency of Japan?<br/>";
echo   "<a href=\"?answer=1\"  target='_self'	> A. Yen </a><br/>";  
echo   "<a href=\"?answer=2\"  target='_self'	> B. Rupees </a><br/>";  
echo   "<a href=\"?answer=3\"  target='_self'	> C. Dollar</a><br/>";  

# If you create more page for  next question
# echo   "<a href='###.php'  target='_self'	> Next Question </a><br/>";  

echo  "</center>";

?>
 
 
 <?php
  if( $flag==true)
 {
 ?>
 <div  style="width:400px; margin:auto; background-color:#C66B6D; text-align:center"> 
 <?php echo $status?>
 </div>
 <?php
   }
 ?>
 </body>
</html>

output..

cc