Cannot Redeclare Function
Fatal error: Cannot redeclare formatDate() (previously declared in helpers.php:10)Quick Answer
You are defining a function that already exists. This usually happens when a file is included multiple times. Use require_once or include_once, or wrap the function in a function_exists check.
Why This Happens
PHP does not allow two functions with the same name in the same scope. This error typically occurs when a file containing function definitions is included multiple times via require or include, or when two different files define a function with the same name.
The Problem
// index.php
require 'helpers.php';
require 'helpers.php'; // Included twice
// helpers.php
function formatDate($date) {
return $date->format('Y-m-d');
}The Fix
// index.php
require_once 'helpers.php'; // Only included once
// helpers.php
function formatDate($date) {
return $date->format('Y-m-d');
}Step-by-Step Fix
- 1
Find both declarations
The error message tells you the function name and where it was previously declared. Search your codebase for all declarations of that function.
- 2
Switch to require_once
Replace require and include with require_once and include_once to prevent files from being loaded multiple times.
- 3
Use namespaces or classes
Organize functions into namespaced classes or use Composer autoloading to avoid manual file inclusion entirely.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free