Re: Method compare()

From: Date: Wed, 10 Aug 2016 00:33:21 +0000
Subject: Re: Method compare()
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-94981@lists.php.net to get a copy of this message
On 08/09/2016 12:33 PM, David Rodrigues wrote:
Currently PHP has a method called "version_compare()". Should be great if we have the same function but to general usage. There are some implementations on web for that, but I guess that it could be done natively.
compare(mixed $a, mixed $b, string $operator): boolean
Then I could do something like that:
compare(1, 2, '>') === false compare('1', 1, '===') === false
It should be very useful to libraries that expects that user pass an operator. For instance:
$collection->where('id', '>', 5) => compare($id, 5, '>')
I guess that it could supports all PHP operators: ==, ===, !=, <>, !==, <, >, <= and >=. The spaceship operator does the works too for integers, but not should works if you expects than $a === $b, or when you expects than $a >= $b.
See the Comparable RFC for something similar: https://wiki.php.net/rfc/comparable -- Stephen

« previous php.internals (#94981) next »