3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /* Script showing that reset() takes its parameter by reference */ if (defined('HHVM_VERSION')) { exit; } // adapted from http://php.net/manual/en/language.references.php#99644 $g_refcount_added = 0; function refcount($var) { ob_start(); debug_zval_dump($var); $dump = ob_get_clean(); $matches = array(); preg_match('/refcount\(([0-9]+)/', $dump, $matches); $count = $matches[1]; global $g_refcount_added; return $count - $g_refcount_added; } // configure our refcount function $array = array('key' => 'value'); $refcount_expected = 1; $refcount_actual = refcount($array); // 3 in PHP 7.0, 4 in other versions $g_refcount_added = $refcount_actual - $refcount_expected; unset($refcount_actual, $refcount_expected, $array); // end of configure our refcount function function print_refcounts($label, $orig_rc, $copy_rc = null) { if ($copy_rc === null) { printf("%s : orig %d %s (%s)\n", $label, $orig_rc, " ", 'alone'); } else { printf("%s : orig %d, copy %d (%s)\n", $label, $orig_rc, $copy_rc, $copy_rc > 1 ? 'sharing' : 'separated'); } } $orig = array('foo' => 'bar'); print_refcounts("initially ", refcount($orig)); $copy = $orig; print_refcounts("after copy", refcount($orig), refcount($copy)); reset($copy); // if the argument is passed by reference, PHP separates $copy and $orig print_refcounts("after call", refcount($orig), refcount($copy));
Output for 7.1.0 - 7.3.0
initially : orig 1 (alone) after copy : orig 2, copy 2 (sharing) after call : orig 1, copy 0 (separated)
Output for 4.3.0 - 5.6.38, 7.0.0 - 7.0.33
initially : orig 1 (alone) after copy : orig 2, copy 2 (sharing) after call : orig 1, copy 1 (separated)
Output for hhvm-3.10.1 - 3.17.3