|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-01-14 13:24 UTC] scottmacvicar at ntlworld dot com
Description:
------------
strlen will return a value for boolean types as well as a consitent value of 5 for an array.
Reproduce code:
---------------
<?php
var_dump(strlen('foo')); // 3
var_dump(strlen(true)); // 1
var_dump(strlen(false)); // 0
var_dump(strlen(array('foo', 'bar'))); // 5
?>
Expected result:
----------------
string foo = int(3)
bool true = int(-1)
bool false = int(-1)
array('foo', 'bar') = int(-1)
Actual result:
--------------
string foo = int(3)
bool true = int(1)
bool false = int(0)
array('foo', 'bar') = int(5)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 07:00:02 2025 UTC |
Patch diff -u zend_builtin_functions.c zend_builtin_functions.c.patched --- zend_builtin_functions.c 2003-08-28 18:06:52.000000000 +0100 +++ zend_builtin_functions.c.patched 2004-01-14 17:46:02.000000000 +0000 @@ -260,6 +260,16 @@ if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &str) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } + switch((*str)->type) { + case IS_LONG: + case IS_DOUBLE: + case IS_STRING: + break; + default: + zend_error(E_WARNING, "Variable passed to strlen() is not a string or number"); + RETURN_LONG(-1); + break; + } convert_to_string_ex(str); RETVAL_LONG((*str)->value.str.len); }As PHP is a typeless language, this behaviour is expected. Please take a look at this extended version of your script: var_dump((string)'foo', strlen('foo')); // 3 var_dump((string)true, strlen(true)); // 1 var_dump((string)false, strlen(false)); // 0 var_dump((string)array('foo', 'bar'), strlen(array('foo', 'bar'))); // 5