|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-06-23 23:23 UTC] todd at magnifisites dot com
Description:
------------
The substr() function will convert an integer and return the correct results. Using strlen() returns NULL, it does not convert an integer to string so that the expected results are returned.
Reproduce code:
---------------
$string = '12345';
$number = 12345;
print substr($number, -1) . '<br />'; // works fine
print $string{strlen($string)-1} . '<br />'; works fine
// This won't work, $number is not a string:
print $number{strlen($number)-1} . '<br />';
// Now cast it as a string and it will:
$number = (string)$number;
print $number{strlen($number)-1} . '<br />';
Expected result:
----------------
I would expect PHP to automagically convert the integer to a string without having to cast it as string first.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 14:00:01 2025 UTC |
It's not strlen() that's failing. It's the $number{$offset} syntax. I can see the argument for casting $number to a string when string offset syntax is used, but I'm not sure enough it's the right thing to do.