php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28902 String offset syntax can't be used on integers.
Submitted: 2004-06-23 23:23 UTC Modified: 2004-06-24 18:55 UTC
From: todd at magnifisites dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.3.8-dev OS: All
Private report: No CVE-ID: None
 [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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-24 01:51 UTC] pollita@php.net
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.
 [2004-06-24 02:38 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is intended behavior. 
 [2004-06-24 18:55 UTC] todd at magnifisites dot com
Ah yes, my misunderstanding.  Thanks pollita for the explanation and clarifying my mistake.  Consider the (non) issue closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC