PHP switch

Summary: in this tutorial, you will learn about the PHP switch statement that executes a code block by matching an expression with multiple values.

Introduction to the PHP switch statement

When the value of a single variable determines the number of different choices, you can use the if...elseif statement.

Suppose that you’re building a website whose users have many roles like admin, editor, author, and subscriber.

The following example uses an if elseif statement to display a different message based on the role of the user:

<?php

$role = 'subscriber';
$message = '';

if ('admin' === $role) {
	$message = 'Welcome, admin!';
} elseif ('editor' === $role) {
	$message = 'Welcome! You have some pending articles to edit';
} elseif ('author' === $role) {
	$message = 'Welcome! Do you want to publish the draft article?';
} elseif ('subscriber' === $role) {
	$message = 'Welcome! Check out some new articles.';
} else {
	$message = 'Sorry! You are not authorized to access this page';
}

echo $message;
Code language: HTML, XML (xml)

Output:

Welcome! Check out some new articles.Code language: JavaScript (javascript)

When the value of a single variable specifies the number of different choices, it’s much cleaner to use the switch statement like this:

<?php

$role = 'admin';
$message = '';

switch ($role) {
	case 'admin':
		$message = 'Welcome, admin!';
		break;
	case 'editor':
		$message = 'Welcome! You have some pending articles to edit';
		break;
	case 'author':
		$message = 'Welcome! Do you want to publish the draft article?';
		break;
	case 'subscriber':
		$message = 'Welcome! Check out some new articles.';
		break;
	default:
		$message = 'You are not authorized to access this page';
}

echo $message;Code language: HTML, XML (xml)

The following illustrates the syntax of the switch statement:

<?php

switch (expression) {
	case value1:
		// code block 1
		break;
	case value2:
		// code block 2
		break;
	case value3:
		// code block 3
		break;
	default:
		// default code block
}
Code language: HTML, XML (xml)

The switch statement compares an expression with the value in each case.

If the expression equals a value in a case, e.g., value1, PHP executes the code block in the matching case until it encounters the first break statement.

If there’s no match and the default is available, PHP executes all statements following the default keyword.

In case the default is not specified, and there’s no match, the control is passed to the statement that follows the switch statement.

The following flowchart illustrates how the switch statement works:

PHP switch

Combining cases

Since PHP executes the switch statement from the matching case label till it encounters the break statement, you can combine several cases in one.

The following example uses the switch statement and combines the cases of 'editor' and 'author':

<?php

$message = '';
$role = 'author';

switch ($role) {
	case 'admin':
		$message = 'Welcome, admin!';
		break;
	case 'editor':
	case 'author':
		$message = 'Welcome! Do you want to create a new article?';
		break;
	case 'subscriber':
		$message = 'Welcome! Check out some new articles.';
		break;
	default:
		$message = 'You are not authorized to access this page';
}

echo $message;Code language: HTML, XML (xml)

Output:

Welcome! Do you want to create a new article?Code language: PHP (php)

In this example, if the role is editor or author, it’ll show the same message.

PHP switch statement’s alternative syntax

PHP also supports the alternative syntax for the switch statement as follows:

<?php

switch (expression):
	case value1:
		// code block 1
		break;
	case value2:
		// code block 2
		break;

	default:
		// default code block
		break;
endswitch;Code language: HTML, XML (xml)

The alternative syntax is suitable for mixing with the HTML code.

Summary

  • Use PHP switch statement instead of a series of if statements on the same expression.
Did you find this tutorial useful?