Skip to content

Conversation

hikari-no-yume
Copy link
Contributor

This adds three constants to PHP:

const CMP_LT = -1;
const CMP_EQ = 0;
const CMP_GT = 1;

Why? Code readability. It means you can replace the magic numbers with more human-readable names, and prevent misunderstandings (particularly as to what strcmp($a, $b) === 0 does).

For example:

switch ($a <=> $b) {
    case CMP_LT:
        ...
    case CMP_EQ:
        ...
    case CMP_GT:
        ...
}

or

if (CMP_EQ === strcmp($a, $b)) {
    ...
}

@hikari-no-yume
Copy link
Contributor Author

Ah, hmm, this doesn't work out. strcmp('a', 'c') is -2. Nevermind.

@rquadling
Copy link
Contributor

It would be really nice to have CMP_LTZ, CMP_EQZ, CMP_GTZ, but this is a bit "macro-y".

Or, if you were feeling perverse, introduce three new callbacks for comparisons ...

strcmp($a, $b, callable $ltz, callable $eqz, callable $gtz)

But that is SO Javascript.

Introducing the spaceship operator is great, but you still have to do several comparisons against the result to do anything useful.

@hikari-no-yume
Copy link
Contributor Author

Or just do <= 0 and >= 0

@datibbaw
Copy link
Contributor

I suppose normalising all comparators to return only -1, 0 and 1 isn't going to happen any time soon? ;-)

@hikari-no-yume
Copy link
Contributor Author

Probably not, someone must rely on it...

@hikari-no-yume
Copy link
Contributor Author

Surprised me, though. I would've thought strcmp was normalised.

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