|  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
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:
Solve the problem:
48 + 5 = ?
Subscribe to this entry?

 [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: Wed Oct 28 15:01:23 2020 UTC