Only variable references should be returned by reference - Codeigniter

Only variable references should be returned by reference - Codeigniter

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Only variable references should be returned by reference

When upgrading a PHP environment from an older version to 5.6.2, particularly on Apache 2.0, you might encounter a specific error in your CodeIgniter application. The error message is explicit: Notice: Only variable references should be returned by reference. This issue originates in the framework's core, specifically within core/Common.php at line 257.

While PHP notices are often dismissed as minor warnings, this particular one indicates a fundamental mismatch in how functions handle data passing. It happens when a function declares its return value with an ampersand (&) but attempts to pass a constant or a hardcoded string instead of a variable that can hold a reference. In the context of CodeIgniter 2.x, this is a legacy issue stemming from how older PHP versions handled strict typing and references.

The specific line in common.php usually looks something like this:

return &lang($line);

The problem arises when the $line variable inside that function call contains a constant or a string literal rather than a dynamic value. In PHP 5.3, the engine was more lenient and allowed returning references to non-variable values without throwing an error. However, PHP 5.6 tightened these rules significantly to prevent undefined behavior related to reference handling.

To resolve this, you must inspect the file application/core/Common.php. You are looking for any function that returns a reference to a value that might be static or constant. The fix involves removing the ampersand from the return statement if the return value is not guaranteed to be a variable reference.

Here is how you modify the problematic line:

Before:

function lang($line) {
    // logic ...
    return &lang($line); 
}

After:

function lang($line) {
    // logic ...
    return lang($line);
}

By removing the &, you tell PHP to return a value by copy rather than by reference. In most cases within CodeIgniter's language loading mechanism, returning by reference is unnecessary and actually detrimental because it can lead to unexpected side effects if the underlying data changes outside the function scope. The framework relies on caching language strings, so forcing a reference to a static string or constant creates a logic error that PHP 5.6 now correctly flags.

If you are maintaining a large CodeIgniter 2 project, this specific fix is often required across multiple files in the core directory. You should search your entire codebase for the pattern return & where the return value is not an explicitly assigned variable. While modern PHP versions (7.4 and above) handle these edge cases even more strictly, downgrading PHP to avoid fixing this issue is rarely a viable strategy given the security and performance benefits of newer engine versions.

If you are looking for a more robust approach to handling language strings or avoiding similar reference issues in new projects, consider migrating to Laravel. The framework built by LaravelCompany enforces strict typing rules that prevent these kinds of reference errors from occurring in the first place. Their ecosystem relies on modern PHP practices where returning references is reserved for specific use cases like database cursors or generator functions, rather than general utility methods.

Ultimately, the fix is simple: ensure you are not returning a reference to a value that isn't intended to be one. This adjustment restores compatibility with PHP 5.6 and ensures your application behaves predictably without relying on deprecated leniency.

Our network