“Fatal error: Cannot redeclare

前端 未结 17 967
[愿得一人]
[愿得一人] 2020-11-22 05:26

I have a function(this is exactly how it appears, from the top of my file):



        
相关标签:
17条回答
  • 2020-11-22 06:16

    This errors says your function is already defined ; which can mean :

    • you have the same function defined in two files
    • or you have the same function defined in two places in the same file
    • or the file in which your function is defined is included two times (so, it seems the function is defined two times)

    I think your facing problem at 3rd position the script including this file more than one time.So, you can solve it by using require_once instead of require or include_once instead of include for including your functions.php file -- so it cannot be included more than once.

    0 讨论(0)
  • 2020-11-22 06:19

    If your having a Wordpress theme problem it could be because although you have renamed the theme in your wp_options table you havn't renamed the stylesheet. I struggled with this.

    0 讨论(0)
  • 2020-11-22 06:20

    In my case it was because of function inside another function! once I moved out the function, error was gone , and everything worked as expected.

    This answer explains why you shouldn't use function inside function.

    This might help somebody.

    0 讨论(0)
  • 2020-11-22 06:21

    Solution 1

    Don't declare function inside a loop (like foreach, for, while...) ! Declare before them.

    Solution 2

    You should include that file (wherein that function exists) only once. So,
    instead of :  include ("functions.php");
    use:             include_once("functions.php");

    Solution 3

    If none of above helps, before function declaration, add a check to avoid re-declaration:

    if (!function_exists('your_function_name'))   {
      function your_function_name()  {
        ........
      }
    }
    
    0 讨论(0)
  • 2020-11-22 06:21

    I had the same problem. And finally it was a double include. One include in a file named X. And another include in a file named Y. Knowing that in file Y I had include ('X')

    0 讨论(0)
提交回复
热议问题