|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69931 substr() behaviour change
Submitted: 2015-06-25 08:44 UTC Modified: 2015-06-25 21:20 UTC
From: Assigned: nikic (profile)
Status: Not a bug Package: Strings related
PHP Version: 7.0Git-2015-06-25 (Git) OS: Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2015-06-25 08:44 UTC]
The substr() function behaves differently in a current PHP7 than e.g. in alpha1

Test script:
substr("Hello", 5, 1)

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-25 08:48 UTC]
I believe this is what's causing the change:

@@ -2406,7 +2407,7 @@ PHP_FUNCTION(substr)

-       if (f >= (zend_long)str->len) {
+       if (f > (zend_long)str->len) {

...which was introduced in 257054e81d4dad73bf9d09cd206d3a6727ad1777 to fix #62922

 [2015-06-25 08:51 UTC]
-Assigned To: +Assigned To: nikic
 [2015-06-25 10:48 UTC]
-Status: Assigned +Status: Not a bug
 [2015-06-25 10:48 UTC]
This change is intentional. The behavior is of course BS, but this is because strpos() in general returns BS. E.g. substr("Hello", 4, 20) has always returned "o" rather than throwing an error. The change only allowed 5 as a valid start offset here (which it is).
 [2015-06-25 21:20 UTC]
I support this change.
However, this change may bite people. It should be clearly noted in UPGRADING.
A company that I know of was bitten by strtotime() return value change by PHP 5.1 and lost few million dollars, for example.
 [2015-08-07 12:49 UTC]
Automatic comment from SVN on behalf of cmb
Log: updated substr() documentation for PHP 7 (see #69931)
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 19 11:01:30 2024 UTC