PHP Spotlight: Match

Written by NewTrick

15 Jul 2025

#PHP

An illustration of a stuffed PHP elephant placed on the keyboard of a laptop.

PHP's match is a cool little expression that allows evaluation based on the identity check of a value. It's a bit like switch, but instead of checking for weak equality (==), it will check for identity (===).

Used correctly, it has the potential to clean up long switch statements.

Here is a basic example, using identity matching. Note the use of default which will catch anything not anticipated by the match statement.

<?php

function identity_match($i) {
   $is_it_a_match = match($i) {
       'tea' => 'Tea is available',
       'milk' => 'Milk is available',
       'coffee' => 'Coffee is available',
       'juice' => 'Juice is available',
       default => 'That is not available'
   };
   return $is_it_a_match;
}

var_dump(identity_match('coffee')); //returns 'Coffee is available'
var_dump(identity_match('vodka')); //returns 'That is not available'

?>

Match can also used for non-identity checks like truthiness. Below we can check the value of a variable, to see if a person is old enough to drink alcohol:

<?php

function is_it_a_truthy_match($i) {
   $return_value = match(true) {
       $i < 18 => 'Not old enough to drink alcohol',
       $i >= 18 => 'Old enough to drink alcohol',
       default => 'Not a vaild age',
   };
  return $return_value;
}
var_dump(is_it_a_truthy_match(19)); //returns 'Old enough to drink alcohol'
var_dump(is_it_a_truthy_match(1)); //returns 'Not old enought to drink alcohol'

?>

 

However, we need to ensure that we have account for the truthiness being checked, as if we don't handle our types properly, as we can see if we were to use the following in the function above:


var_dump(is_it_a_truthy_match('Bananas')); //returns 'Old enought to drink alcohol'
var_dump(is_it_a_truthy_match(true)); //returns 'Old enough to drink alcohol'
var_dump(is_it_a_truthy_match('')); //returns 'Not old enought to drink alcohol'

 

So if we tighten up the control for types, we can check safely:

 

<?php

function is_it_a_truthy_match(int $i): string {
   $return_value = match(true) {
       $i < 18 => 'Not old enough to drink alcohol',
       $i >= 18 => 'Old enough to drink alcohol',
       default => throw new InvalidArgumentException('Not a vaild age'),
     };
  return $return_value;
 }
function safe_age_check($input) {
   try {
       return is_it_a_truthy_match($input);
   } catch (TypeError $e) {
       return 'Invalid input: expected an integer';
   }
}
//Check out the variety of responses!
var_dump(is_it_a_truthy_match(19)); //returns 'Old enough to drink alcohol'
var_dump(is_it_a_truthy_match(1)); //returns 'Not old enought to drink alcohol'
var_dump(is_it_a_truthy_match('Bananas')); //returns type error
var_dump(safe_age_check(20)); //returns 'Old enough to drink alcohol'
var_dump(safe_age_check(5)); //returns 'Not old enought to drink alcohol'
var_dump(safe_age_check('')); //returns 'Invalid input: expected an integer'
var_dump(safe_age_check('Bananas')); //returns 'Invalid input: expected an integer'

?>

 

I'm sure there are lots of other examples thought where testing for truthiness in this way might lead to unexpected results.

As long as we control for this, match is a super useful expression that can help keep out code clean.

References:
https://www.php.net/manual/en/control-structures.match.php