The Hidden Cost of Passing Arrays by Reference in Legacy PHP Code
The error message Strict Standards: Only variables should be passed by reference is a classic artifact of evolving PHP standards, often surfacing when refactoring older scripts into modern environments like XAMPP or production servers running PHP 7.4+. It stems from a specific pattern involving functions that expect their arguments to be modified directly within the function scope, yet developers are passing arrays instead of variables.
In the scenario provided, the code attempts to parse filenames using explode(), specifically looking at lines 32 and 34 where $extension and $fileName are assigned via array operations. The culprit lies not in the logic itself, but in how these assignments are handled by PHP's strict typing rules introduced in later versions.
Why Arrays Break Reference Passing
The root cause is the interaction between array_pop() and array_shift() with their arguments. These functions require their first argument to be passed by reference (indicated by an ampersand &) because they modify the original array by removing elements. When you write:
$extension = strtolower(array_pop(explode(".", $value)));
You are passing $value to explode(). While explode() does not require a reference, the result is stored in an unnamed variable (implicitly) before being passed to array_pop(). However, older versions of this code pattern often looked like this:
// Old style that might trigger warnings in strict mode if misinterpreted
$extension = &strtolower(array_pop(explode(".", $value)));
But even without the explicit ampersand in your snippet, modern PHP versions with error_reporting set to include E_STRICT flag the assignment because the internal implementation of array_pop() and array_shift() expects a variable bound by reference. If the function signature implies a destructive operation on the input, passing a non-variable (or a value that isn't explicitly treated as a variable in this context) triggers the warning.
In your specific case, the error occurs because you are likely running code written for PHP 5.x where these functions handled references differently, or you have passed an array by reference in a way that conflicts with newer strict standards. The explode() function returns a new array; array_pop() takes that array and removes the last element. In modern PHP, you must ensure you are passing a variable to these destructive functions, not a literal or a value that cannot be altered by reference.
Refactoring the Logic
To resolve this without relying on deprecated behavior, you should stop trying to use array_pop() and array_shift() on the result of explode(). Instead, calculate the filename and extension using string manipulation or index access on the resulting array without destroying the original data in a way that triggers strict warnings.
Here is a robust approach that separates the logic cleanly:
$parts = explode('.', $value);
$extension = strtolower(array_pop($parts));
$fileName = implode('.', $parts); // Reconstruct the name without the extension
// Check file types
if (!in_array('*', $fileTypes) && !in_array($extension, $fileTypes)) {
continue;
}
$results[] = '<li class="file ' . $extension . '">';
$results[] .= '<a href="' . str_replace('\\', '/', $directory) . '/' . $value . '">' . $displayName($fileName, $extension) . '</a></li>';
This approach avoids passing anything by reference incorrectly because we are explicitly working with a new variable ($parts) that holds the exploded result. We do not pass $value directly to array_pop(). By storing the exploded array first, we ensure compatibility with modern PHP versions and eliminate the Strict Standards warning entirely.
Trade-offs and Best Practices
One of the main trade-offs here is performance versus clarity. Using explode() multiple times or reconstructing strings can introduce overhead in very high-throughput applications. However, for file listing scripts, readability and correctness take precedence over micro-optimizations. The original code's reliance on side effects (modifying arrays in place) is a legacy pattern that often leads to bugs when combined with strict error reporting.
If you are maintaining a Laravel project or using a modern framework, always enable strict standards during development. This forces you to write cleaner code that doesn't rely on implicit references. If you encounter this error, check your php.ini file:
error_reporting = E_ALL | E_STRICT
display_errors = On
Running the script with these settings enabled will expose any remaining reference issues immediately. While the link to https://laravelcompany.com offers resources on modern PHP practices, understanding these low-level reference mechanics remains essential when debugging legacy integrations or custom utility scripts like recursive directory listings.
