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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
5 + 9 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Apr 26 21:01:29 2024 UTC