php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53252 Invalid type (boolean) returned by substr() when providing an empty string
Submitted: 2010-11-07 13:48 UTC Modified: 2010-11-07 20:54 UTC
From: cagret at gmail dot com Assigned: frozenfire (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.3 OS: Linux, Windows
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: cagret at gmail dot com
New email:
PHP Version: OS:

 

 [2010-11-07 13:48 UTC] cagret at gmail dot com
Description:
------------
substr() returns boolean false when providing an empty string as first argument, 
but it should return an empty string "".

Example:
<?php
$s = substr('', 0, 1);
echo gettype($s);
?>

Output:
boolean (false)

There is an *error in php documentation*:
http://pl.php.net/manual/en/function.substr.php

See the example for the third argument "Length":
>> $rest = substr("abcdef", 4, -4);  // returns ""

That is not true, $rest contains FALSE and not "".

Cheers,
Cezary Tomczak


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-07 14:08 UTC] felipe@php.net
-Package: Strings related +Package: Documentation problem
 [2010-11-07 14:08 UTC] felipe@php.net
The example in the documentation must be fixed.

And about your example, we have an example in the documentation that is just like your case:

var_dump(substr('a', 1)); // bool(false)

That is the expected behavior.
 [2010-11-07 16:00 UTC] frozenfire@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: frozenfire
 [2010-11-07 16:21 UTC] philip@php.net
5.2.2 broke this (or fixed) but it's a BC issue. Before this time, it returned "".
 [2010-11-07 16:23 UTC] philip@php.net
I referred to the documentation example above. Passing in an empty string always 
returned false.
 [2010-11-07 16:28 UTC] frozenfire@php.net
-Status: Assigned +Status: Closed
 [2010-11-07 16:28 UTC] frozenfire@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2010-11-07 16:28 UTC] philip@php.net
-Status: Closed +Status: Assigned
 [2010-11-07 16:28 UTC] philip@php.net
Sorry, I'm being unclear this early morning.

It's $rest = substr("abcdef", 4, -4) that changed with 5.2.2, the other examples 
here have always been false. The change probably relates to PHP Bug #40754

In the very least, the substr() documentation needs a changelog entry.
 [2010-11-07 20:53 UTC] frozenfire@php.net
Automatic comment from SVN on behalf of frozenfire
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=305175
Log: Indicated change to substr's behaviour with start positions beyond truncation positions.
Added changelog indicating this BC break. Bug #53252.
 [2010-11-07 20:54 UTC] frozenfire@php.net
-Status: Assigned +Status: Closed
 [2010-11-07 20:54 UTC] frozenfire@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC