Runtime Error
Cannot pass parameter 2 by reference in /var/www/html/api.php on line 56
What This Error Means
This error occurs when trying to pass a value by reference in a function parameter that has been declared to be passed by value. In PHP, it's generally not possible to pass a value by reference in a function if the parameter is declared as a variable, array, or object, because these types are passed by reference by default.
Why It Happens
This error can occur when a developer tries to use the & operator to pass a value by reference to a function, but the function parameter has been declared without the & symbol. This can also happen when using PHP's default argument passing behavior for variables, arrays, or objects, which is to pass them by reference.
How to Fix It
- 1To fix this error, modify the function parameter to include the & symbol to declare it as a reference. Alternatively, restructure the code to avoid passing a value by reference if it's not necessary. Here's an example of how to fix the code:
- 2// Before (broken code)
- 3function update_user_data(&$user_id, $user_data) {
- 4 // Code here
- 5}
- 6// After (fixed code)
- 7function update_user_data($user_id, &$user_data) {
- 8 // Code here
- 9}
Example Code Solution
function calculate_sum($array) {
$result = 0;
foreach ($array as $num) {
$result += $num;
}
return $result;
}function calculate_sum($array) {
$result = 0;
foreach ($array as &$num) {
$result += $num;
}
return $result;
}Fix for Cannot pass parameter 2 by reference in /var/www/html/api.php on line 56
Browse Related Clusters
Related PHP Errors
Undefined variable: user_id in /var/www/html/dashboard.php on line 25
Cannot send headers. Output started at /var/www/html/index.php:123
Undefined property: stdClass::$email in /var/www/html/user.php on line
Notice: Trying to access array offset on value of type null in /var/ww
Related PHP Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error