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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue May 07 06:01:30 2024 UTC