|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79662 substr($string, $offset, null) should return from $offset till end
Submitted: 2020-06-01 20:41 UTC Modified: 2020-06-01 20:44 UTC
From: thflori at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Strings related
PHP Version: 7.4.6 OS: Linux/Any
Private report: No CVE-ID: None
 [2020-06-01 20:41 UTC] thflori at gmail dot com
Usually when you don't want to pass an optional argument you pass null. That does not only feel natural for me I hope. But not with substr - when you pass null it is the same as when you pass 0. It is clearly described in the documentation as well.

This looks like no problem for the most of the users. Obviously just don't pass it. But no one knows what the developers out there might develop; maybe the third parameter comes from another call and you have to write an exception for substr:


if ($length === null) 
  $sub = substr($string, $offset);
  $sub = substr($string, $offset, $length);

I'm not sure is it really intended to be that way or are there only historical reasons?

From manual page:

Test script:

$string = 'lorem ipsum';
$passNull = substr($string, 6, null);


Expected result:
string(5) "ipsum"

Actual result:
string(0) ""


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-01 20:44 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-06-01 20:44 UTC]
This has already been addressed in PHP 8. As this is part of a large set of default value handling improvements, and is technically backwards compatibility breaking, I don't think we want to backport this to earlier versions.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 24 17:01:30 2024 UTC