Skip to content
PHP Logo

Are there potential issues or pitfalls when omitting the & operator in a PHP function?

Omitting the & operator in a PHP function when passing arguments by reference can lead to unexpected behavior, as the function will create a copy of the variable instead of modifying the original value. To solve this issue, make sure to use the & operator before the parameter in the function definition to pass it by reference.

php
function modifyValue(&$value) {
    $value *= 2;
}

$num = 5;
modifyValue($num);
echo $num; // Output will be 10