How to load return array from a PHP file?

How to load return array from a PHP file?

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

Stefan Bogdanescu

Founder & Senior Architect

Why include Doesn't Return Arrays and How to Fix It

When you attempt to load a PHP file containing a raw array definition using include, the operation fails because of how the PHP interpreter handles statements versus expressions. The code snippet provided in the source material defines a function named fetchArray that attempts to include a file and implicitly expects a value to be returned. However, the include statement does not automatically return the result of the last evaluated expression within the included file.

In the example you provided, the PHP file ends with a closing parenthesis for the array. While this looks like a valid expression, the surrounding code structure dictates execution flow. If the included file is structured as:

return array(
    'key' => 'value',
    'key2' => 'value2'
);

The include statement executes the file's contents. The return keyword inside that file tells PHP to exit the current function scope and send a value back to the caller. However, when you use include($filename) outside of a function context, or if the included file is intended to be executed as a standalone script (common with translation files), the return statement has no effect on the global execution flow. It effectively does nothing because there is no active function scope to return to.

To correctly load this array into a variable, you must explicitly capture the output of the inclusion or refactor how the data is loaded. The most robust approach for loading configuration or translation arrays from external files is to treat the file as a callable factory.

Refactoring the Included File

The simplest fix involves modifying the included file to ensure it returns the array regardless of whether it is invoked via include or evaluated directly. If the source file is strictly a data container, you cannot rely on side effects like global variable assignment (e.g., $GLOBALS['config'] = ...) because that pollutes the namespace and makes testing difficult.

Instead, modify the external PHP file to define a function or class method that returns the array:

// config_messages.php
return [
    'key' => 'value',
    'key2' => 'value2'
];

Wait, there is a nuance here. A bare return at the top level of a script only works if you evaluate the file's contents as an expression. When you use include, it executes statements sequentially. If your file contains only the array definition without assignment to a variable, and that definition starts with return, PHP expects it inside a function.

The correct pattern for a standalone config file is to wrap the array in a function or assign it to a constant/variable before returning:

// config_messages.php
function getMessages() {
    return [
        'key' => 'value',
        'key2' => 'value2'
    ];
}

Then, update your loader function to call this specific entry point:

function fetchArray($in)
{
    require_once $in; // Use require_once to prevent re-inclusion errors on multiple calls
    return getMessages();
}

Alternative: Using include with Evaluation

If you cannot modify the source file to add a function, you must evaluate the included code in a way that captures its output. However, this is dangerous and generally discouraged for translation files because it executes arbitrary code.

A safer alternative if the file structure is fixed is to assign the result of the inclusion to a variable if the file returns a value. But since include executes statements, not expressions, you usually need to change the included file to define a function that returns the data.

If the source file (like a Yii message translation file) is strictly meant to be included and has no return statement at the top level, you can't "load" it via include directly into a variable without changing how the data is exposed. In legacy frameworks like Yii, these files often use a specific convention where the entire file content is expected to be part of a larger configuration array construction.

Best Practice for Translation Files

For translation files specifically, especially in contexts similar to Yii or Laravel, the standard practice is not to treat them as raw PHP arrays to be loaded via include. Instead, you should load them into an associative array and iterate over them, or use a framework-specific loader.

If you are maintaining a legacy codebase where these files exist as standalone PHP snippets returning arrays, the only reliable way to extract that data is to refactor the included file to define a function (as shown above). This ensures that fetchArray can return the data cleanly without relying on global state or implicit behavior.

Using require instead of include is also a consideration. include generates a warning if the file is missing, while require throws a fatal error. For configuration files, which are critical for application startup, require (or require_once) is often preferred to fail fast rather than silently continuing with missing translations or config data.

Finally, avoid using eval() to parse these files. While eval might technically allow you to execute the array expression and capture the result if wrapped in parentheses, it introduces severe security risks and performance overhead. Stick to structured function calls or dedicated configuration loaders provided by your framework.

Our network