@ 2017-07-03T14:46:46Z <?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));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 4.4.1 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 initially : orig 1 (alone)
after copy : orig 2, copy 2 (sharing)
after call : orig 2, copy 2 (sharing)
Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
initially : orig 1 (alone)
after copy : orig 2, copy 2 (sharing)
after call : orig 2, copy 2 (sharing)
Output for 4.3.0 - 4.3.11 , 4.4.0 , 4.4.2 - 4.4.9 , 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.30 initially : orig 1 (alone)
after copy : orig 2, copy 2 (sharing)
after call : orig 1, copy 1 (separated)
preferences:dark mode live preview
250.33 ms | 404 KiB | 370 Q