php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26292 substr returns "0" for any offset on the string "0"
Submitted: 2003-11-17 17:16 UTC Modified: 2003-11-17 17:54 UTC
From: ravacholp at hotmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.3.2 OS: windows 2000
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:
45 - 1 = ?
Subscribe to this entry?

 
 [2003-11-17 17:16 UTC] ravacholp at hotmail dot com
Description:
------------
substr() returns "0" for any offset on the string "0" using the length 1.

This causes an infinite loop if you are checking for leading zeros in your string using substr().

Reproduce code:
---------------
$str = "0";

print (substr($str, 0, 1) == "0");
print (substr($str, 1, 1) == "0");

print (substr($str, 5, 1) == "0");

// only the first line should evaluate to true

Expected result:
----------------
111


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-17 17:54 UTC] sniper@php.net
With a more reliable test script:

# cat t.php 
<?php
$str = "0";
var_dump(substr($str, 0, 1));
var_dump(substr($str, 1, 1));
var_dump(substr($str, 5, 1));
?>

# php t.php 
string(1) "0"
bool(false)
bool(false)

No bug here.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 01:01:28 2024 UTC