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 & MySQL | 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

🤖

ChatGPT Free
Ask questions on any topic

CITE ARTICLE


For homework, research, thesis, books, magazines, blogs or academic articles

APA Format Reference:

Delgado, Hugo. (2019).
Switch in PHP.
Retrieved Apr 26, 2024, from
https://disenowebakus.net/en/switch-php

Participates!

Share it with your friends 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.

CONTINUE LEARNING

PREVIOUS


IT ALSO DESERVES TO PAY TO VISIT:

Not finding what you need?

Use our internal search to discover more information
Related content:

Would you like to learn more about Web Design?

Meet all the courses and tutorials that we have for you completely free
Learn Web Design
 

Leave your Comment

SPONSOR

Your business can also appear here. More information

Next article:

Your browser has blocked advertising.
Please 🙏 allow to visualize the announcements to be able to access, thank you.