PHP Match Expression Is The New And Improved Switch Statement

Kumar Ravi
3 min readMar 3, 2023

--

Let’s admit it, we mostly do not prefer to write Switch statements. No matter how many if else, if else, we write, unless it is very much needed we ignore the switch statement in the PHP world.

The reason can be many, for me the reason it its confusing structure and the fact that after every case I have to write a break to stop it from further going into the statement.

I believe the PHP team recognised it and now in PHP 8, we have a match expression which is an enhancement over the traditional switch statement.

I have published a video to show you some examples of how it works so If you prefer to watch a video you can watch it here.

Or if you prefer to read it let's begin our journey here.

Let’s say there is an evil AI that wanted to check if there is any human present on the planet.

if yes then return human present else return no human present, we can write it with a match expression like this.

<?php

class EvilAI{

public function checkHuman(){
$speciesPresentNow = 'human';
return match($speciesPresnetNow){
'human' => 'human present',
default => 'no species found'
};
}
}

now if we make an object of EvilAI and call its function checkHuman it will return us human present.

$evilAI = new EvilAI();
$result = $evilAI->checkHuman();

There are some things to talk about it here.

First, the match expression uses === ( identity check), which means it will compare boolean with boolean and string with string, we can not mix different types.

so if(true === “true”) will not pass since one is of type boolean and one is of type string.

second, the default case match expression throws UnhandledMatchError exception if there is no default case provided, so if no default case then we will have to put it inside try catch and handle the exception by ourselves.

Now let's say this AI wanted to perform some action and call the nukeThePlanetAgain function if there are any humans found, we can write it like this.

<?php

class EvilAI{

public function checkHuman(){
$speciesPresentNow = 'human';
return match($speciesPresnetNow){
'human' => $this->nukeThePlanetAgain(),
default => 'no human species found'
};
}

public function nukeThePlanetAgain(){
return 'making sure no human survived';
}
}

match expression returns the value of whatever match is found so in the above case it will return making sure no human survived.

Now the matching condition can also be a function and above can be rewritten like this.

<?php

class EvilAI{

public function checkHuman(){
return match(true){
$this->areYouHuman() => $this->nukeThePlanetAgain(),
default => 'no human species found'
};
}

public function nukeThePlanetAgain(){
return 'making sure no human survived';
}

public function areYouHuman(){
return true;
}
}

Look if we are returning true to an AI that is asking us that are we human then for sure we deserve to be nuked 😂.

We can also use multiple comma-separated calls which will work as or condition like this.

<?php

class EvilAI{

public function checkHuman(){
return match(true){
$this->areYouHuman(),$this->isThisEarth() => $this->nukeThePlanetAgain(),
default => 'no human species found'
};
}

public function nukeThePlanetAgain(){
return 'making sure no human survived';
}

public function areYouHuman(){
return true;
}
public function isThisEarth(){
return true;
}
}

we can also use comparisons for the same like this.

<?php

class EvilAI{

public function checkHuman(){
return match(true){
$this->howManyTimesThisPlanetIsNuked() < 5 => $this->nukeThePlanetAgain(),
default => 'no human species found'
};
}

public function nukeThePlanetAgain(){
return 'making sure no human survived';
}

public function howManyTimesThisPlanetIsNuked(){
return 2;
}
}

let's be honest, no human can survive 5 times nuking of the entire planet 🤣

There are plenty of other ways of using the match expression and I see a lot of its usage in the latest PHP packages, so please get yourself familiar with this function, you will thank me later.

Let me know if it helped you by clapping the article, Thanks for reading 🙏

--

--