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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

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 04:01:30 2024 UTC