Syntax

PHP Match Expression Syntax

In PHP, match expression: returns a value based on strict matches — $label = match ($n) { 1 => 'one', default => 'other', };

Quick Answer

Syntax
$label = match ($n) { 1 => 'one', default => 'other', };
PHPSyntax

Match Expression

Code example
$label = match ($n) { 1 => 'one', default => 'other', };

Returns a value based on strict matches.

Learn More

How to match expression in PHP

  1. Open your PHP file or editor.
  2. Copy the example, then replace its sample names and values for your code.

PHP Match Expression Syntax

Use $label = match ($n) { 1 => 'one', default => 'other', };.

Related Syntax References

Frequently asked questions

What is the PHP syntax for match expression?

Use this pattern: $label = match ($n) { 1 => 'one', default => 'other', };

Can I copy this PHP code example?

Yes. Use Copy code and adapt the example to your program.

Is this PHP syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify