PHPWarningRuntime ErrorApril 26, 2026

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

  1. 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. 2// Before (broken code)
  3. 3function update_user_data(&$user_id, $user_data) {
  4. 4 // Code here
  5. 5}
  6. 6// After (fixed code)
  7. 7function update_user_data($user_id, &$user_data) {
  8. 8 // Code here
  9. 9}

Example Code Solution

❌ Before (problematic code)
PHP
function calculate_sum($array) {
    $result = 0;
    foreach ($array as $num) {
        $result += $num;
    }
    return $result;
}
✅ After (fixed code)
PHP
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

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error