|  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
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.
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-2020 The PHP Group
All rights reserved.
Last updated: Tue Dec 01 03:01:24 2020 UTC