Skip to content

Conversation

@fulldecent
Copy link
Contributor

Above in this text it states:

match expression arms may contain multiple expressions separated by a comma. That is a logical OR, and is a short-hand for multiple match arms with the same right-hand side.

So in the code example below we prefer to use the comma instead of a logical OR.

Copy link
Contributor

@PhilDaiguille PhilDaiguille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the , instead of || is not quite equivalent, when the individual functions do not return a boolean. It works for str_contains(), but will not work for preg_match() for example:

https://3v4l.org/X7fQZ

I wonder if this example should perhaps be completely removed for that reason. Or alternatively, each of the cases should be cast to boolean in some way 🤔

@fulldecent
Copy link
Contributor Author

Wow, so if uses a == true to evaluate truth. But match uses === true. I'd call that a bug.

Check out this https://3v4l.org/h78ue

@TimWolla
Copy link
Member

Wow, so if uses a == true to evaluate truth. But match uses === true. I'd call that a bug.

That is an intentional design decision made in the RFC for the match() expression: https://wiki.php.net/rfc/match_expression_v2#no_type_coercion

@fulldecent
Copy link
Contributor Author

That RFC relies on an old version of PHP (7) to make an argument.

PHP 8 fixed that issue: https://3v4l.org/PLkiG


Thanks for the references and education here.

@kamil-tekiela
Copy link
Member

Using the , instead of || is not quite equivalent, when the individual functions do not return a boolean. It works for str_contains(), but will not work for preg_match() for example:

https://3v4l.org/X7fQZ

I wonder if this example should perhaps be completely removed for that reason. Or alternatively, each of the cases should be cast to boolean in some way 🤔

This is a separate problem and I agree that if the example used a function that doesn't return a boolean, the result should be cast to boolean first. But the example uses str_contains which can only return a boolean. In this case, using , is more correct than a boolean ||.

I am going to merge this as I cannot see anything preventing this PR.

@kamil-tekiela kamil-tekiela merged commit 17502eb into php:master Jun 5, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants