Inconsistencies: Arrays And Foreach
HHVM and PHP have a few inconsistencies when it comes to how arrays are handled within
foreach loops, as well as sorting.
Under PHP5, if an array's internal cursor points to the position past the last element and then a copy of the array is made, the copy will have its internal cursor reset to point at the first element. Under HHVM, when a copy of an array is made, the copy's internal cursor will always point to the same position that the original array's internal cursor pointed to.
<?php namespace HHVM\UserDocumentation\Inconsistencies\Intro\Examples\AIC; $x = [0, 1]; next(&$x); // internal array cursor points at 1 next(&$x); // internal array cursor points after the last element $y = $x; $y = 2; // Triggers copy-on-write var_dump(current(&$x)); // bool(false) var_dump(current(&$y)); // int(2) on HHVM, int(0) on PHP
foreach by value will modify the array's internal cursor under certain circumstances. Under HHVM,
foreach by value will never modify the array's internal cursor.
Under PHP5, the behavior of a
foreach by reference loop can be unpredictable if during iteration the next element of the array is unset, or if the array variable is assigned to directly or through a reference, or if copy-on-write causes the array to be copied. For such cases, HHVM's behavior may differ from PHP5.
There are cases when sorting functions in HHVM and PHP may not match (or may be unpredictable in output comparison). The primary cases are as follows:
- When any sorting values compare as equal. (See https://3v4l.org/hMirb)
- When the sorting keys are of mixed type. (See https://3v4l.org/g5G3T)
In general, you shouldn't rely on this implementation-defined behavior, but if you need to, and it must match PHP, there is a runtime INI option you can specify to HHVM