Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Match() With Multiple Values

By Pascal Baljet

<?php

match([$clause, $this->isNested()]) {
    [Clause::IsSet, true]  => $query->has($this->relationship),
    [Clause::IsNotSet, true]  => $query->doesntHave($this->relationship),
    [Clause::IsSet, false]  => $query->whereNotNull($this->column),
    [Clause::IsNotSet, false]  => $query->whereNull($this->column),
};

This is an example of using match(), to match against two values at the same time. The trick is to collect the two, or more, values in an array, and then return an array with the same number of elements in the same order.

It would also be possible to make a match(true), and use boolean expressions for each arm of the match(). The first one that match is used.

See Also

PHP Features

Last updated: 10 September 2026