Is Passing by Reference Faster in PHP (>=5.0)?
Despite the primary purpose of pass-by-reference in PHP being variable manipulation outside the function's scope, one might speculate if it also affects performance. PHP employs Copy On Write to optimize object and array copying, which should eliminate the need for pass-by-reference for parameters that remain unchanged.
Performance Comparison
To determine the performance impact, an experiment was conducted with 100,000 function calls, passing a 20 kB string as a parameter. Two scenarios were tested:
Results
Pass by Value vs. Reference for Reading/Using Parameters:
Pass by Value vs. Reference for Writing/Modifying Parameters:
Conclusions
The above is the detailed content of Is Pass-by-Reference Always Faster in PHP?. For more information, please follow other related articles on the PHP Chinese website!