3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Script to check whether current() takes its parameter by reference or by value */ 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)); current($copy); // if the argument is passed by reference, PHP separates $copy and $orig print_refcounts("after call", refcount($orig), refcount($copy));

preferences:
43.61 ms | 402 KiB | 5 Q