Cannot redeclare a function previously declared

Cannot redeclare a function previously declared

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

Stefan Bogdanescu

Founder & Senior Architect

Cannot redeclare a function previously declared: The Case of Duplicate Includes

When PHP throws a Fatal error: Cannot redeclare ae_detect_ie(), it is screaming one thing above all else: you are trying to load the same code twice. This specific scenario often baffles developers because the syntax looks identical in both locations, and the logic itself is sound. However, the PHP runtime does not allow two functions with the same name to exist in a single request scope, regardless of how different their bodies might be or which one was defined first.

In the error log provided, the conflict arises between /home/xdesign/public_html/Powerful/config.php at line 24 and the same file at line 29. While the snippet shows a function definition starting at line 29, the message explicitly states that ae_detect_ie() was already declared earlier in that exact file. This suggests a structural issue within config.php itself rather than a collision with an external script, unless config.php is being included by multiple files during the same request lifecycle.

Understanding PHP's Function Namespace Scope

PHP treats functions as global entities. Once you define function ae_detect_ie(), that name is locked for the duration of the script execution. If your application architecture involves including config.php from multiple entry points—such as a main index file, an admin panel, or a specific module—the second time PHP encounters the definition, it halts execution immediately.

This behavior differs from variables, which are scoped by their declaration context unless explicitly declared global or static. Functions, however, live in the global function namespace. If you have a legacy system where config.php contains utility functions like user agent detection, and your new framework or another plugin attempts to load that config file again, you hit this wall.

Diagnosing the Duplicate Load

The error message provides the smoking gun: it tells you exactly where the first declaration happened and where the second attempt occurred. In the specific case of ae_detect_ie(), the code checks for MSIE in the user agent string to detect Internet Explorer. This is a classic utility function, likely intended for legacy browser compatibility or redirect logic.

To resolve this, you must ensure config.php is included exactly once per request. Common patterns that lead to this error include:

  1. Multiple require or include statements: If your main application file includes config.php, and a plugin also tries to load it, the second inclusion triggers the fatal error.
  2. Autoloading conflicts: If you have an autoloader that detects the function and loads the config file again, this creates a loop or a duplicate definition.
  3. Refactoring needs: The most robust solution is often to move utility functions out of configuration files. Modern frameworks typically separate configuration (constants, database credentials) from business logic. Functions like ae_detect_ie() belong in a utilities library or a dedicated service class, not alongside database settings.

Refactoring for Modern Architectures

If you are maintaining legacy code found on hosts like the one mentioned in the source material, moving these functions might seem like heavy lifting. However, sticking with them inside config.php invites exactly this type of error. A better approach is to create a separate file, perhaps named utils.php or browserDetection.php, and include that file once from your main bootstrap script.

// utils.php
if (!function_exists('ae_detect_ie')) {
    function ae_detect_ie()
    {
        if (isset($_SERVER['HTTP_USER_AGENT']) && 
            strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
            return true;
        else
            return false;
    }
}

By wrapping the definition in a function_exists() check, you create a safe guard. If the function is already loaded (because another file included it first), PHP skips the definition entirely. This pattern prevents fatal errors while maintaining compatibility with different inclusion orders.

Why It Works on Another Host

The fact that the script works on another host usually indicates that the file structure or the autoloading mechanism differs there. Perhaps on the working host, config.php is only included once, or the specific plugin causing the collision isn't present. On your current host, the path /home/xdesign/public_html/Powerful/config.php is likely being reached multiple times, triggering the conflict.

If you are building a new application and want to avoid these issues entirely, consider using a framework that enforces a strict entry point. Laravel, for instance, handles configuration and service providers in a way that prevents accidental re-inclusion of core logic. For more details on organizing your application structure safely, you might look into how modern frameworks handle service registration.

Ultimately, the fix is architectural: ensure every file defining global functions is loaded exactly once, or protect those definitions with existence checks.

Our network