php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68863 Inconsistent return from substr()
Submitted: 2015-01-20 13:31 UTC Modified: 2015-08-07 11:47 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: scott at smashcat dot org Assigned: cmb (profile)
Status: Closed Package: Strings related
PHP Version: 5.6.4 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: scott at smashcat dot org
New email:
PHP Version: OS:

 

 [2015-01-20 13:31 UTC] scott at smashcat dot org
Description:
------------
Currently the substr() function is inconsistent in its return value. Thus the value of the String object is affecting the type of object / primitive that is returned. Although the input String (shown below) is of zero length, it's still a String. So currently the returned value type needs to be checked by the calling code. To return a consistent type of value I suggest that either the last case should return an empty string, OR the substr() function should return boolean false if the input string is shorter than the required substr.


Test script:
---------------
substr("a",0,10);
substr("a",0,0);
substr("",0,10);
substr("",0,0);

Expected result:
----------------
substr("a",0,10) === false
substr("abc",0,1) === "a"
substr("",0,0) === ""
substr("",0,1) === false


Actual result:
--------------
substr("a",0,10) == "a"
substr("a",0,0) == ""
substr("",0,10) == false
substr("",0,0) == false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-25 02:09 UTC] yohgaki@php.net
I would like to have

substr("a",0,10) == "a"
substr("a",0,0) == ""
substr("",0,10) == ""
substr("",0,0) == ""

It would be nicer not to return FALSE for string and return maximum chars available upto specified length.

What others think?
 [2015-01-30 03:18 UTC] debian at michael-neubert dot de
I would not change the current behaviour. Too many PHP code is written based on that results. And the documentation is clear:

substr("a",0,10); -> returns "a", I would expect that. If you do not like that, make an extra length() check before.

substr("a",0,0); -> returns empty string, I epxect that, see documentation:
http://php.net/manual/en/function.substr.php
"If length is given and is 0, FALSE or NULL an empty string will be returned."

substr("",0,10); and substr("",0,0);  -> returns false, I would expect that, because the given input string is emtpy. There is no starting point, so false correct.

The input variables are checked in order from the first to the last argument. So for me the behaviour is as expected and this is no bug and nothing should be changed.
 [2015-01-30 03:21 UTC] debian at michael-neubert dot de
As addition from the manual:
http://php.net/manual/en/function.substr.php

"If string is less than or equal to start characters long, FALSE will be returned."

This is for the 3rd and 4th of the given examples.
 [2015-08-07 11:47 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-08-07 11:47 UTC] cmb@php.net
The behavior of substr() has been changed as of PHP 7.0.0alpha2:
<http://3v4l.org/RhdvD>. The documentation is work in progress.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jun 19 13:01:23 2021 UTC