Match Function Php 8

  • 25-05-2022
  • 1593
  • PHP
  • Haresh Chauhan

The Match expression evaluate base on identity matching value. This will return a match value expression from the function. it also allows multiple value match inside match expression.

Match will not work lower than the 8 PHP version.

It can be used instead of if and else if and switch case conditions. also you can use this match expression with multiple self logical conditional.

Match expression will return the value matched expression.

This new match expression is a powerful feature that will often be a better choice instead of a switch case.

The Match expression is same use as we using before Switch case statement but this one is better then other and just key different.

Example: 1

Instead of if() and else if() you can easy to use Match you can see in this example given. The user name identifies the position of the person in the company.

$name = 'Rickey';

if ($name == 'Micky') {

    $whoIs = 'Manager';
}
else if($name == 'Rickey') {

    $whoIs = 'Sub-Admin';
}
else if($name == 'Bunty'){

    $whoIs = 'Security';
}else{

    $whoIs = 'Staff';
}

// MATCH FUNCTION

$whoIs = match ($name) {
    'Micky' => 'Manager',
    'Rickey' => 'Sub-Admin',
    'Bunty' => 'Security',
    default => 'Staff'
};

// OUTPUT

// Sub-Admin

Here above example Rickey name person finding position in the company. Both conditions will give you the same identity.

Example: 2

In this example used Match expression instead of Switch Case both outputs with the given example will be the same. So here better use of Match function.

$name = 'Micky';

switch ($name) {
    case 'Micky':
        $whoIs = 'Manager';
    break;
    case 'Rickey':
        $whoIs = 'Sub-Admin';
    break;
    case 'Bunty':
        $whoIs = 'Security';
    break;
	default:
        $whoIs = 'Staff';
    break;
}

// MATCH FUNCTION

$whoIs = match ($name) {
    'Micky' => 'Manager',
    'Rickey' => 'Sub-Admin',
    'Bunty' => 'Security',
    default => 'Staff'
};

// OUTPUT

// Manager

Using match above example finding person position in the company.

Example: 3

Also you can use Match expression as default. Before you are using in Switch case function. If the match function can`t find the value from the given expression then it will take the default value from the expression.

$name = 'Bunty`s';

$whoIs = match ($name) {
    'Micky' => 'Manager',
    'Rickey' => 'Sub-Admin',
    'Bunty' => 'Security',
    default => 'Staff'
};

// OUTPUT DEFAULT 

// Security

Above Example, you can see Bunty`s not available in the Match that's why is it returning Staff as it is the default.

Example: 4

Match will also check multiple values and return. You can see in the example inside match there were two names at first position 'Micky', 'Rickey'. If any single value matches then also will return.

$name = 'Rickey';

$whoIs = match ($name) {
    'Micky','Rickey' => 'Manager OR Sub-Admin',
    'Bunty' => 'Security',
    default => 'Staff'
};

// OUTPUT

// Manager OR Sub-Admin

The above example will give us a return if any single value match with the given param.

Example: 5

This example is also important from the developer's point of you, you can see that Here match param is true. So if any expression will be true then it will return that matching expression value.

$age = 23;

$result = match (true) {
    $age >= 65 => 'senior',
    $age >= 25 => 'adult',
    $age >= 18 => 'young adult',
    default => 'kid',
};

// OUTPUT

// young adult

Thank you for reading our post.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :