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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ravacholp at hotmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sun Dec 22 05:01:30 2024 UTC