3v4l.org

run code in 300+ PHP versions simultaneously
<?php // plain variable $a = 42; // array containing a reference $b = [ 'a' => &$a ]; // capture the array by-value $f = function() use($b) { // update the reference from inside the closure $b['a'] = 69; }; // call it $f(); // observe that both the array and the plain variable now have the new value var_dump($a, $b);
Output for 7.4.0 - 7.4.30, 8.0.1 - 8.0.22, 8.1.0 - 8.1.9
int(69) array(1) { ["a"]=> &int(69) }

preferences:
33.94 ms | 409 KiB | 5 Q