Switch in PHP

Conditional structures called switch are said to be "selective", since they select one of several possible paths according to the value of a variable.

Switch in PHP | Learn PHP | Conditional structures called switch are said to be "selective", since they select one of several possible paths according to the value of a variable

Choose between known values of a single variable: The switch

Conditional structures called a switch are said to be "selective", since they select one of several possible paths according to the value of a variable:

To program a switch, it is placed in parentheses, next to the word switch, the variable or matrix cell whose possible values we know and we want to find out, in each execution of the page, what value they have to execute accordingly. Let's see this other example, which evaluates a variable called $ day (which is expected to be given as value one of the days of the week):

<?php
switch($day){
// here we will evaluate the possible values of $ day
}?>

PHP switch flow chart

Case (each possible value)

Inside the switch keys, after the variable to be evaluated, a series of case blocks are repeated (a possible "case", a possible value of that variable) as many times as possible values we hope that the variable examined has. That is, a case for each value. As only one of these cases will be executed, within the, we have simply defined a value for the $ text variable and, in the end, we will show that variable, whose content -and, therefore, the text that will be displayed- will have taken one or the other value, as evaluated in the switch, according to which case turned out to be true. Add the cases to the example:


<?php
	switch($day) {
		case "Monday":
		$text="Happy day of the Moon!";
		break;
	
		case "Tuesday":
		$text="Happy Mars day!";
		break;
	
		case "Wednesday":
		$text="Happy mercury day!";
		break;
		
		case "Thursday":
		$text="Happy Jupiter day!";
		break;
		
		case "Friday":
		$text="Happy day of Venus!";
		break;
		
		case "Saturday":
		$text="Happy Saturn day!";
		break;
		
		case "Sunday":
		$text="Happy day of the sun!";
		break;
	}
	print ($ text);
?>

Let's note the syntax: after the word case, we will wrap the value of the variable in quotes, and then we put two points. All the commands of the PHP language that come next, until the word "break", will be executed only if that is the value of the variable $ day evaluated.

As we see, this structure only serves us at home to know the possible exclusionary alternatives (all possible values of the variable that will be evaluated).

Default

Additionally, we can offer a default alternative (although it is optional, and may not be included, it is much better to always include an action by default).

The case of the previous example has no action planned for the case in which $ day has a different value to the seven defined values.

To avoid the risk of this logical "vacuum", it is used in the last place of the case sequence, or rather, after the last case, the default expression (default). The code wrapped in that block will be executed in any other case in which the evaluated variable contains anything else not foreseen in any of the previous cases:


<?php
	switch ($ day) {
		case "Monday":
		$text="Happy day of the Moon!";
		break;
		
		case "Tuesday":
		$text="Happy Mars day!";
		break;
		
		case "Wednesday":
		$text="Happy mercury day!";
		break;
		
		case "Thursday":
		$text="Happy Jupiter day!";
		break;
		
		case "Friday":
		$text="Happy day of Venus!";
		break;
		
		case "Saturday":
		$text="Happy Saturn day!";
		break;
		
		case "Sunday":
		$text="Happy day of the sun!";
		break;
		
		default:
		$text="Happy day off calendar!";
		break;
	}
	
	print ($ text);

We can try loading different values within the $ day variable to see how one or another case is executed, or the default

Did you like it or was it useful?

Help us share it in Social Networks

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

IT ALSO DESERVES TO PAY TO VISIT:

PREVIOUS


Elseif in PHP

Not finding what you need?

Use our internal search to discover more information
Sponsored content:
 

Leave your Comment

SPONSOR

Your business can also appear here. More information