# Expose compare_function() to userspace

- Thread: https://externals.io/message/49815
- Original archive: https://news-web.php.net/php.internals/49815
- Started by Adam Harvey on 2010-09-30T14:44:01+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #49815 — Adam Harvey — 2010-09-30T14:44:01+00:00

URL: https://externals.io/message/49815#49815

Well, while I'm sending e-mails...

Unrelated to the Comparable RFC, are there any objections to adding a
function that simply wraps compare_function()? It's depressingly
common to end up writing a construct like the following in comparison
callbacks, so I think we might as well encapsulate the pattern in an
actual function:

if ($a < $b) {
  return -1;
}
elseif ($a > $b) {
  return 1;
}
return 0;

If I don't hear kicking and screaming in the next few days, I'll slip
this into trunk. I can provide a patch/RFC in advance if there's
demand, but it'll be about as simple as you imagine. :)

The one thing that I would like bikeshedding^Wfeedback on is the name:
cmp() makes the most sense to me, since it lines up well with Python
and (to a lesser extent) Perl, but if there are concerns about
potential name clashes with existing code, may I suggest
var_compare(). Get your votes and/or suggestions in!

Thanks,

Adam

---

## #49819 — Richard Quadling — 2010-09-30T15:11:39+00:00

URL: https://externals.io/message/49815#49819 · In reply to: #49815

> Well, while I'm sending e-mails...
>
> Unrelated to the Comparable RFC, are there any objections to adding a
> function that simply wraps compare_function()? It's depressingly
> common to end up writing a construct like the following in comparison
> callbacks, so I think we might as well encapsulate the pattern in an
> actual function:
>
> if ($a < $b) {
>  return -1;
> }
> elseif ($a > $b) {
>  return 1;
> }
> return 0;
>
> If I don't hear kicking and screaming in the next few days, I'll slip
> this into trunk. I can provide a patch/RFC in advance if there's
> demand, but it'll be about as simple as you imagine. :)
>
> The one thing that I would like bikeshedding^Wfeedback on is the name:
> cmp() makes the most sense to me, since it lines up well with Python
> and (to a lesser extent) Perl, but if there are concerns about
> potential name clashes with existing code, may I suggest
> var_compare(). Get your votes and/or suggestions in!
>
> Thanks,
>
> Adam

  [5] => strcmp
  [6] => strncmp
  [7] => strcasecmp
  [8] => strncasecmp
  [99] => variant_cmp
  [403] => strnatcmp
  [404] => strnatcasecmp
  [1211] => gmp_cmp

vs

     [431] => substr_compare
     [869] => version_compare
     [1302] => collator_compare
     [1428] => ldap_compare

var_cmp() looks a LOT like variant_cmp() (Windows only - a function in
the COM/DOTNET extension)

and

variable_compare($a,$b) is actually longer than ($a<$b?-1:(($a>$b)1:0)

But var_cmp() would be my +1

Richard.
-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

---

## #49828 — Stas Malyshev — 2010-09-30T20:13:49+00:00

URL: https://externals.io/message/49815#49828 · In reply to: #49815

Hi!

> If I don't hear kicking and screaming in the next few days, I'll slip
> this into trunk. I can provide a patch/RFC in advance if there's
> demand, but it'll be about as simple as you imagine. :)

Sounds good.

> The one thing that I would like bikeshedding^Wfeedback on is the name:
> cmp() makes the most sense to me, since it lines up well with Python
> and (to a lesser extent) Perl, but if there are concerns about
> potential name clashes with existing code, may I suggest
> var_compare(). Get your votes and/or suggestions in!

I'd like var_compare().
Quick search in google reveals cmp() being used in actual code, and 
compare() to some extent too, though mostly as method, so not really 
clashing.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #49830 — Pierre Joye — 2010-09-30T22:13:35+00:00

URL: https://externals.io/message/49815#49830 · In reply to: #49828

hi,



> I'd like var_compare().
> Quick search in google reveals cmp() being used in actual code, and
> compare() to some extent too, though mostly as method, so not really
> clashing.

Sounds good.

Can we make it an operator-like (is_a for example) instead? It could
be more efficient.

Cheers,
--
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #49831 — Stas Malyshev — 2010-09-30T22:26:41+00:00

URL: https://externals.io/message/49815#49831 · In reply to: #49830

Hi!

> Can we make it an operator-like (is_a for example) instead? It could
> be more efficient.

Operator has a downside that you can't pass it as a parameter. We could 
have something like <=> (spaceship operator!) or even cmp though :)
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #49833 — Adam Harvey — 2010-10-01T02:52:09+00:00

URL: https://externals.io/message/49815#49833 · In reply to: #49831

> > […]
>
> Operator has a downside that you can't pass it as a parameter. We could have
> something like <=> (spaceship operator!) or even cmp though :)

Yeah, I think if it's going to be available, it has to be as a
function so it can be used as a callback.

I can cook up a patch for an operator version in addition to the
functional version, although I don't know that the performance benefit
would necessarily be worth the extra complication (or potential
confusion for users of having two ways to skin the same cat).

Adam

---

## #49838 — Michael Wallner — 2010-10-01T09:57:38+00:00

URL: https://externals.io/message/49815#49838 · In reply to: #49831

> Hi!
>
> > […]
>
> Operator has a downside that you can't pass it as a parameter. We
> could have something like <=> (spaceship operator!) or even cmp
> though :)

OT: Stas, could you please include the name of the Person(s) you're
quoting in your mails.  It's extremely hard to follow the conversation
if there are posts of you in it.

Thank you!
Mike

---

## #49839 — Hannes Magnusson — 2010-10-01T16:45:44+00:00

URL: https://externals.io/message/49815#49839 · In reply to: #49815

> Well, while I'm sending e-mails...
>
> Unrelated to the Comparable RFC, are there any objections to adding a
> function that simply wraps compare_function()? It's depressingly
> common to end up writing a construct like the following in comparison
> callbacks, so I think we might as well encapsulate the pattern in an
> actual function:
>
> if ($a < $b) {
>  return -1;
> }
> elseif ($a > $b) {
>  return 1;
> }
> return 0;

I'm a bit confused...

Are you proposing a new PHP function to check if 2 integers are equal?
The usecase being a callback function for u*sort()?

What happens when it gets passed an string? or an object with a
__toString() ? the string "100$ USD"?

I am a bit skeptical here..
Sure, I have written that code snippet millions of times, but I have
also written that between() snippet million times..
Don't think it deserves to become a PHP core function though.
When I write the function I can atleast control what happens with
non-ints.. like if ($a instanceof ValueObject) { $a->getNumeric() ..

Can't see the point of adding the trillionth "helper" function into PHP.

-Hannes

---

## #49845 — Richard Lynch — 2010-10-03T11:47:24+00:00

URL: https://externals.io/message/49815#49845 · In reply to: #49815

> Unrelated to the Comparable RFC, are there any objections to adding a
> function that simply wraps compare_function()? It's depressingly
> common to end up writing a construct like the following in comparison
> callbacks, so I think we might as well encapsulate the pattern in an
> actual function:
>
> if ($a < $b) {
>   return -1;
> }
> elseif ($a > $b) {
>   return 1;
> }
> return 0;
>
> If I don't hear kicking and screaming in the next few days, I'll slip
> this into trunk. I can provide a patch/RFC in advance if there's
> demand, but it'll be about as simple as you imagine. :)
>
> The one thing that I would like bikeshedding^Wfeedback on is the name:
> cmp() makes the most sense to me, since it lines up well with Python
> and (to a lesser extent) Perl, but if there are concerns about
> potential name clashes with existing code, may I suggest
> var_compare(). Get your votes and/or suggestions in!

I don't really think we need this in core.

I mean, yeah, I may have written the same function a few times, but
it's not exactly brain surgery. (which I've been through so I know :-)

Except for handling non-int input, which you probably won't do in the
core function anyway...

Seems kind of a pointless bloat to this developer.

-1

PS
It should be INT_cmp if it takes INT.  Or num_cmp or number_cmp

var_cmp et al would imply any sort of var...

Though I guess using < and > means it would do whatever those
operators do for Objects, strings, or resources...  Which I wouldn't
even want to try to predict and rely on anyway.

-- 
brain cancer update:
http://richardlynch.blogspot.com/search/label/brain%20tumor
Donate:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FS9NLTNEEKWBE
