|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78101 Incorrect behaviour for substr on Windows
Submitted: 2019-06-03 18:48 UTC Modified: 2019-07-12 15:12 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mishamosher at gmail dot com Assigned: nikic (profile)
Status: Closed Package: opcache
PHP Version: 7.2.19 OS: Windows 10 1903 64-bits
Private report: No CVE-ID: None
 [2019-06-03 18:48 UTC] mishamosher at gmail dot com
I am trying this simple code:

echo substr("/es", 0, 1);

I expect to see a "/" as a result, but I get "�" instead.

PHP is running in an entirely 64-bit environment.

 - Windows 10 1903
 - PHP 7.2.19 (as an apache module)
 - Apache 2.4.39

Running PHP from the console (php.exe -f script.php) functions properly.

Test script:
echo substr("/es", 0, 1);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-03 19:23 UTC]
-Status: Open +Status: Not a bug
 [2019-06-03 19:23 UTC]
Either you typed an invisible character (copy/pasted a BOM?) or that slash is a Unicode slash made up of multiple bytes.

If you intend to support multibyte strings and non-ASCII encodings then use mbstring and mb_substr() instead.
 [2019-06-03 20:55 UTC] mishamosher at gmail dot com

I appreciate your attention into the matter.

I know for sure that there are no multibyte strings or invisible characters in my testing. Functions strlen, mb_strlen and mb_detect_encoding return 3, 3 and "ASCII" respectively.

In the mantime, I've just finished configuring PHP 7.2.11 and PHP 7.3.6, as Apache modules, and they both do not exibit this issue with the very same test file and test environment.

The real mistery for me is why specifically PHP 7.2.19 does not want to work as it should. Please note that the issue presents itself only when I run PHP 7.2.19 as an Apache module. The issue does not occur when I run PHP via CMD (php.exe -f script.php).

Is there any specific test or debug information that I can provide to further identify the issue?
 [2019-06-03 21:24 UTC]
I tested it on Windows using the following script:

  substr("/es", 0, 1)
  substr("\xEF\xBB\xBF/es", 0, 1)


string(1) "/"
string(1) "�"

In general try using var_dump() on the source string. It will report the byte length, so you will notice invisible characters.
 [2019-06-04 01:00 UTC] mishamosher at gmail dot com

Are your results from running the script with PHP as an Apache module?

I've thoroughly tried over and over again, and no matter what, I always get the wrong results.

var_dump reports <<string(3) "/es">>.

I've been able to pin-point the issue to the OPCache extension. If I simply remove "zend_extension=opcache" from php.ini, everything works as it should.

Please note that I'm experiencing this issue only with PHP 7.2.19 when it is working as an Apache module.

I can use PHP 7.2.11, 5.6.40, 7.3.6, etc., with OPCache enabled and everything works OK in there.

As a final note, what can I do to 'ring' attention to an existing filed bug? It is about this one: - at least to know if it is a bug or intended behaviour.
 [2019-06-04 01:56 UTC]
-Status: Not a bug +Status: Re-Opened -Package: *General Issues +Package: opcache
 [2019-06-04 01:56 UTC]
If removing opcache makes your problem go away then this may be a bug after all.
 [2019-06-04 09:02 UTC]
-Status: Re-Opened +Status: Feedback -Assigned To: +Assigned To: cmb
 [2019-06-04 09:02 UTC]
Please run the following variation of your script, and report the

    $in = "/es";
    $out = substr($in, 0, 1);
    var_dump(bin2hex($in), bin2hex($out));
 [2019-06-04 17:59 UTC] mishamosher at gmail dot com
-Summary: Icorrect behaviour for substr on Windows +Summary: Incorrect behaviour for substr on Windows -Status: Feedback +Status: Assigned
 [2019-06-04 17:59 UTC] mishamosher at gmail dot com

Results with OPCache on:
string(6) "2f6573" string(2) "ff"

Results with OPCache off:
string(6) "2f6573" string(2) "2f"
 [2019-06-06 12:22 UTC]
-Assigned To: cmb +Assigned To:
 [2019-07-12 15:04 UTC]
I want to check back whether this issue still exists in PHP 7.2.20. The 7.2.19 release had some serious issues with opcache and it may be that this is a side-effect of the same problems.
 [2019-07-12 15:08 UTC] mishamosher at gmail dot com
I confirm that this issue is no longer present in PHP 7.2.20 for my environment.
 [2019-07-12 15:12 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-07-12 15:12 UTC]
Thanks for the confirmation! In that case I'm closing this issue...
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 01:01:32 2024 UTC