php.net |  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
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: thflori at gmail dot com
New email:
PHP Version: OS:

 

 [2020-06-01 20:41 UTC] thflori at gmail dot com
Description:
------------
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:

<?php

if ($length === null) 
  $sub = substr($string, $offset);
else
  $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: https://php.net/function.substr
---


Test script:
---------------
<?php

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

var_dump($passNull);

Expected result:
----------------
string(5) "ipsum"

Actual result:
--------------
string(0) ""

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-01 20:44 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-06-01 20:44 UTC] nikic@php.net
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-2020 The PHP Group
All rights reserved.
Last updated: Sun Nov 29 08:01:23 2020 UTC