PHPWarningRuntime ErrorMay 12, 2026

Runtime Error

Cannot pass string by reference as a wrapper in /var/www/html/api.php on line 120

What This Error Means

This error occurs when attempting to use a string as a reference type, which is not allowed in PHP. It typically happens when using the 'by reference' keyword (&) with a string variable.

Why It Happens

This error is usually caused by a misunderstanding of PHP's variable types. When using 'by reference', PHP expects a variable that can be modified directly, but strings are immutable and cannot be passed by reference.

How to Fix It

  1. 1To fix this error, replace the string variable with a variable that can be modified, such as an array or an object. Alternatively, remove the 'by reference' keyword (&) and pass the string as a value instead. For example:
  2. 2// Before (broken code)
  3. 3function update_string(&$str) {
  4. 4 $str = 'new string';
  5. 5}
  6. 6uupdate_string('original string');
  7. 7// After (fixed code)
  8. 8function update_string($str) {
  9. 9 return 'new string';
  10. 10}
  11. 11uupdate_string('original string');

Example Code Solution

❌ Before (problematic code)
PHP
function update_string(&$str) {
    $str = 'new string';
}
uupdate_string('original string');
✅ After (fixed code)
PHP
function update_string($str) {
    return 'new string';
}
uupdate_string('original string');

Fix for Cannot pass string by reference as a wrapper in /var/www/html/api.php on line 120

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error