bool in_array ( mixed needle, array haystack [, bool strict] )

    See the last parameter? :)

    --Jani

On Wed, 14 Dec 2005, Greg Beaver wrote:


Hi all,

I realize that 1 == '01' and 1 == '1.', but why is this next code also
bool(true)?

<?php
var_dump('1.' == '01');
?>

In this case, a string is explicitly being compared to a string, and yet
both are being converted to an int prior to comparison.  I can
understand that implicit type conversion is needed when comparing
strings to ints, but this can be dangerous.  phpDocumentor, for instance
uses this code:

if (in_array(substr($ltrimword, 0, 2), array('1.', '0.')))

and assumes that it will only match either '1.' or '0.', but in fact, it
will match '01', '1.', '0.', '00', and as such if this happens to be
encountered, it can cause an infinite loop in phpDocumentor eating up
all memory (bad - will be fixed in next release).

Of course, I can fix this by using === for each element instead of
in_array(), but this is extremely non-intuitive and dangerous behavior
in the underlying PHP implementation.  The manual says nothing about
auto-conversion of strings to ints when comparing two strings that
satisfy is_numeric().  My question is whether this is a documentation or
an internal PHP error?

Greg

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to