How to fix “Fatal Error: cannot redeclare a function” in PHP

It’s a very common and useful error that happens when:

  • We used the same name in at least two functions.
  • We have the same function defined in at least two places in one file.
  • We included a file with the function more than once.

Here are some ideas on how to fix it:

  • Change the function name.
  • Make sure you didn’t include the same function more than once. For example, use the require_once() instead of require() or include().

Example of properly working function in PHP:


function customFunctionWithUniqueName() {
    return "Hello, I'm the custom function!";

echo customFunctionWithUniqueName();