Switch Case In PHP: A small code example

Share Now

switch (variable) {
	case 'value':
		# code...
		break;
	
	default:
		# code...
		break;
}

#Another code example:

$n = 12;
$r = $n % 2;

switch ($r) {
	case 0:
		echo "{$n} is an even number \n";
		break;
	default:
		echo "{$n} is an odd number";
		break;
}

#Another code example:

switch (date('l')) {
	case "Monday":
		echo "PHP on Monday";
		break;
	case "Tuesday":
		echo "Angular on Tuesday";
		break;
	case "Wednesday":
		echo "Python on Wednesday";
		break;
	case "Thursday":
		echo "Node on Thursday";
		break;
	case "Friday":
		echo "Laravel on Friday";
		break;
	case "Saturday":
		echo "WordPress on Saturday";
		break;
	case "Sunday":
		echo "React js on Sunday";
		break;
	default:
		echo "I don't know what day it is";
		break;
 }
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert