This is for the programmers who are using functions to drive a website in PHP or there are some instances when you are using a common file which has some utility functions in it. In this kind of scenario the same file loops around on different pages and there exists this “Fatal error: Cannot redeclare function”. This error will be encountered if you define a function in the following fashion.



Not many of you might face this problem since we use classes and functions are sandwiched inside them with some scope. But to avoid this kind of error you can define the same function in the following fashion,with a check before defining it.


As you see we just check if the function already exists with the help of a PHP’s inbuilt function function_exists(). Hence by using the above methodology you can avoid this fatal error and your code won’t break.

Hope that helps.

Stay Digified !!
Sachin Khosla

Share this post: