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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 + 34 = ?
Subscribe to this entry?

 
 [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: Thu Dec 03 01:01:23 2020 UTC