codescracker


php

PHP Switch



« Previous Tutorial Next Tutorial »


PHP switch statement is basically used to perform different actions based on different conditions provided.

PHP switch statement is used to select one of many blocks of code to be executed. Here is the general form to use switch statement in PHP:

switch(expression)
{
	case this1:
		code to be executed here, if expression = this1;
		break;  
	case this2:
		code to be executed here, if expression = this2;
		break;
	default:
		code to be executed here,
		if expression is different 
		from both this1 and this2;
}

PHP switch Example

Here is an example, uses switch statement in PHP:

<!DOCTYPE html>
<html>
<body>

<?php

	echo "<h1>Checking for day</h1>"."<br/>";

	$d=date("D");

	switch ($d)
	{
		case "Mon":
			echo "Today is Monday";
			break;
		case "Tue":
			echo "Today is Tuesday";
			break;
		case "Wed":
			echo "Today is Wednesday";
			break;
		case "Thu":
			echo "Today is Thursday";
			break;
		case "Fri":
			echo "Today is Friday";
			break;
		case "Sat":
			echo "Today is Saturday";
			break;
		case "Sun":
			echo "Today is Sunday";
			break;
		default:
			echo "Wonder which day is this ?";
	}

?>

</body>
</html>

Here is the output of the above PHP script:

PHP Switch Statement

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test