"Cannot redeclare function X()" in PHP — What It Means and How to Fix It
What the PHP error "Cannot redeclare function X()" means and how to fix it.
Published October 31, 2026
PHP found two functions with the same name. Usually the same file is included twice, or two libraries define the same helper.
What it means and why it happens
Using include or require multiple times, or copying a function into two files, triggers it. Wrapping in function_exists or using namespaces prevents clashes.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
Fatal error: Cannot redeclare helper() (previously declared in utils.php:3)
Practical examples
Code that triggers it
<?php
require 'utils.php';
require 'utils.php'; // function helper() declared twiceThe fix
<?php
require_once 'utils.php';
// or in utils.php
if (!function_exists('helper')) { function helper() { /* ... */ } }Use namespaces
namespace App\Support;
function helper() { /* no clash with global helper() */ }Common mistakes
- Using require instead of require_once.
- Defining helpers in several packages without checks.
- Not using Composer's files autoload.
Key takeaways
- Cannot redeclare function: PHP found two functions with the same name.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
require vs require_once?
require_once skips files that were already included.
How do I find which line caused Cannot redeclare function?
The error message and stack trace show the file and line; start there and work backwards.