note 98315 added to function.strlen
| From: | basil at gohar dot us | Date: | Tue, 08 Jun 2010 21:03:09 +0000 |
| Subject: | note 98315 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169953@lists.php.net to get a copy of this message | ||
We just ran into what we thought was a bug but turned out to be a documented difference in behavior
between PHP 5.2 & 5.3. Take the following code example:
<?php
$attributes = array('one', 'two', 'three');
if (strlen($attributes) == 0 && !is_bool($attributes)) {
echo "We are in the 'if'\n"; // PHP 5.3
} else {
echo "We are in the 'else'\n"; // PHP 5.2
}
?>
This is because in 5.2 strlen will automatically cast anything passed to it as a string, and casting
an array to a string yields the string "Array". In 5.3, this changed, as noted in the
following point in the backward incompatible changes in 5.3
(http://www.php.net/manual/en/migration53.incompatible.php):
"The newer internal parameter parsing API has been applied across all the extensions bundled
with PHP 5.3.x. This parameter parsing API causes functions to return NULL when passed incompatible
parameters. There are some exceptions to this rule, such as the get_class() function, which will
continue to return FALSE on error."
So, in PHP 5.3, strlen($attributes) returns NULL, while in PHP 5.2, strlen($attributes) returns the
integer 5. This likely affects other functions, so if you are getting different behaviors or new
bugs suddenly, check if you have upgraded to 5.3 (which we did recently), and then check for some
warnings in your logs like this:
strlen() expects parameter 1 to be string, array given in
/var/www/sis/lib/functions/advanced_search_lib.php on line 1028
If so, then you are likely experiencing this changed behavior.
----
Server IP: 64.71.164.2
Probable Submitter: 63.131.70.98
----
Manual Page -- http://www.php.net/manual/en/function.strlen.php
Edit -- https://master.php.net/note/edit/98315
Del: integrated -- https://master.php.net/note/delete/98315/integrated
Del: useless -- https://master.php.net/note/delete/98315/useless
Del: bad code -- https://master.php.net/note/delete/98315/bad+code
Del: spam -- https://master.php.net/note/delete/98315/spam
Del: non-english -- https://master.php.net/note/delete/98315/non-english
Del: in docs -- https://master.php.net/note/delete/98315/in+docs
Del: other reasons-- https://master.php.net/note/delete/98315
Reject -- https://master.php.net/note/reject/98315
Search -- https://master.php.net/manage/user-notes.php