Skip to content

Prefer comma over || for match conditions #4564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fulldecent
Copy link

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
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
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.

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.

3 participants