3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function vars_are_referenced (&$var1, &$var2) { // What we are doing *will* throw an error (I said it was nasty ;-) ) $oldER = error_reporting(); $oldDE = ini_get('display_errors'); error_reporting(0); ini_set('display_errors', 0); // We need to use output buffering, we don't want to break any existing // buffering so we cache the contents of the buffer $oldBuffer = ob_get_length() ? ob_get_clean() : NULL; // If the values are not identical, definitely not a match if ($var1 !== $var2) return FALSE; // Now we inspect the zval of $var1 ob_start(); debug_zval_dump(&$var1); preg_match('/\brefcount\((\d+)\)(?:\b|$)/', ob_get_clean(), $matches); $var1RefCountBefore = (int) $matches[1]; // If they are the same, this will increase the refcount $temp = &$var2; // Inspect the zval of $var1 again ob_start(); debug_zval_dump(&$var1); preg_match('/\brefcount\((\d+)\)(?:\b|$)/', ob_get_clean(), $matches); $var1RefCountAfter = (int) $matches[1]; // If refcount is now greater, they are the same $result = $var1RefCountAfter > $var1RefCountBefore; // Repopulate the output buffer if necessary if ($oldBuffer !== NULL) { ob_start(); echo $oldBuffer; } // Turn error reporting back to correct level error_reporting($oldER); ini_set('display_errors', $oldDE); return $result; } function test_ref_fail () { $a = 1; $var_name = 'a'; var_dump(vars_are_referenced($GLOBALS['a'], $a)); } function test_ref_success_1 () { global $a; $var_name = 'a'; var_dump(vars_are_referenced($GLOBALS['a'], $a)); } function test_ref_success_2 (&$a) { $var_name = 'a'; var_dump(vars_are_referenced($GLOBALS['a'], $a)); } $a = 1; $b = &$a; var_dump(vars_are_referenced($a, $b)); test_ref_fail(); test_ref_success_1(); test_ref_success_2($a); ?>
Output for 5.4.0 - 5.4.14
Fatal error: Call-time pass-by-reference has been removed in /in/FWgS8 on line 20
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Deprecated: Call-time pass-by-reference has been deprecated in /in/FWgS8 on line 20 Deprecated: Call-time pass-by-reference has been deprecated in /in/FWgS8 on line 29 Warning: ini_set() has been disabled for security reasons in /in/FWgS8 on line 44 bool(true) Warning: ini_set() has been disabled for security reasons in /in/FWgS8 on line 44 bool(false) Warning: ini_set() has been disabled for security reasons in /in/FWgS8 on line 44 bool(true) Warning: ini_set() has been disabled for security reasons in /in/FWgS8 on line 44 bool(true)