Parameter references

Even though it is best to avoid passing a variable by reference to a function in order to avoid altering your application's state outside of the function, PHP 7 makes it possible to pass variables by reference to functions in a highly optimized way even if the reference is a mismatch. Let's take the following code example in order to better understand how PHP 7 is much more efficient in doing so than PHP 5:

// chap3_references.php $start = microtime(true); function test (&$byRefVar) { $test = $byRefVar; } $variable = array_fill(0, 10000, 'banana'); for ($x = 0; $x < 10000; $x++) { test($variable); } $time = microtime(true) - $start; echo 'Time elapsed: ' . $time . PHP_EOL; echo memory_get_usage() . ' bytes' . PHP_EOL; ...

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.