Categories
programming

Fix PHP Fatal error: Cannot redeclare “function name” (previously declared in…

The fatal error above will appear if your code contains two or more PHP functions that have the exact same name.

PHP Fatal error:  Cannot redeclare function_name() (previously declared in /path/to/domain.com/file.php:12) in   /path/to/domain.com/another_included_file.php on line 73

Check if the function name has already been used.

If you find yourself in a situation where the function name may or may not exist, then you can check to see if the name of the function has already been defined.

function function_name(){
     //do something
 }
if(!function_exists('function_name')){
     function function_name(){
         //do something
     }
 }

In the code snippet above, I used the function_exists function to check if function_name already exists as a function. Because it does exist in the example above, the second function is never created.