Re: [PHP-DEV] [RFC] Binary String Comparison

[email protected] (Alain Williams)
Newsgroups php.internals
Organization Parliament Hill Computers Ltd
Message-ID <[email protected]>
On Mon, Aug 18, 2014 at 02:44:36PM +0200, Marc Bennewitz wrote:

> >What exactly is wrong with ===, strcmp(), etc..?
> 
> The question isn't "What's wrong with ===, strcmp()?" but "What's
> wrong with ==, <, >?".
> 
> We have a standard way to compare two operands but currently we do
> some magic things to solve something that don't need to be solved.
> 
> If you would like to compare two pears we currently convert the
> pears into apples and compare two apples and say please use a
> special function to compare two pears. Why?
> 
> There is no numeric context to compare two strings numerically.

I don't want to start a flame war, but perl gets it right, you have 2 sets of
operators 'eq' and '=='.

The choice to have a type juggling '==' was made years ago, that should not change.

If you want '<=' with strings, use strcmp().


There is also a speed cost to type juggling (PHP 5.3.3, 64 bit centos):

$a = "1234";
$b = "ghij"

$a == $b is about 64% slower than $a === $b
strcmp($, $b) is about 163% slower than $a === $b

$a = "abcd";
$b = "ghij"

$a == $b is about 24% slower than $a === $b
strcmp($, $b) is about 168% slower than $a === $b

$a = "abcd";
$b = "4567"

$a == $b is about 20% slower than $a === $b
strcmp($, $b) is about 176% slower than $a === $b

$a = "1234";
$b = "4567"

$a == $b is about 20% slower than $a === $b
strcmp($, $b) is about 162% slower than $a === $b

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.