66

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

<?php
//dirname(getcwd());
function generate_salt()
{
    $salt = '';

    for($i = 0; $i < 19; $i++)
    {
        $salt .= chr(rand(35, 126));
    }

    return $salt;
}
...

And for some reason, I keep getting the error:

Fatal error: Cannot redeclare generate_salt() (previously declared in /Applications/MAMP/htdocs/question-air/includes/functions.php:5) in /Applications/MAMP/htdocs/question-air/includes/functions.php on line 13

I cannot figure out why or how such an error could occur. Any ideas?

T.Todua
  • 53,146
  • 19
  • 236
  • 237
fishman
  • 955
  • 1
  • 7
  • 13

18 Answers18

116

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)

To help with the third point, a solution would be to use include_once instead of include when including your functions.php file -- so it cannot be included more than once.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 2
    Hu ; I suppose the line numbers that are indicated are not "precise" ; ; or, just as a precaution : are you sure you're looking at the "right" file ? – Pascal MARTIN Dec 23 '09 at 16:40
  • This is the only file that has a generate_salt() function. – fishman Dec 23 '09 at 16:42
  • 2
    which means that it's probably being included more than once – Pascal MARTIN Dec 23 '09 at 16:45
  • Yes, I think that's it -- how to fix it, I don't know, as I've set this project up pretty badly. I think should probably restart. – fishman Dec 23 '09 at 16:47
  • 2
    using include_once instead of include (and require_once instead of require) often helps, in that kind of situation : this way, it's PHP itself that makes sure a file is not included more than once – Pascal MARTIN Dec 23 '09 at 16:52
  • This happens commonly. The error message could give a bit more information, such as the locations of the two include or require statements for this particular file. Such information would make it much easier to find the actual location of the problem. – David Spector Dec 05 '17 at 18:51
  • This is a brilliant answer as it covers all the possibilities. I just wanted to add a fourth one: if you are trying to define a function with the same name as a build-in class, for instance `get_class`, you will get the same re-declaration error. – Matt07 Apr 23 '22 at 13:08
  • In case someone else makes the same mistake as me: the variation on reason #3 which I had was that within a function I had an include, which was appropriate, but with the wrong filename (which was a file containing a function definition) – zsalya Aug 14 '22 at 10:48
86

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()  {
    ........
  }
}
T.Todua
  • 53,146
  • 19
  • 236
  • 237
11

You can check first whether the name of your function exists or not before you declare the function:

if (!function_exists('generate_salt'))
{
    function generate_salt()
    {
    ........
    }
}

OR you can change the name of the function to another name.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
ahmed hamdy
  • 5,096
  • 1
  • 47
  • 58
  • 1
    I came across a function in a file that was included multiple times (to generate HTML code), so I used `function_exists` like you suggest, in order to make sure the function was only declared once. – mwfearnley Mar 30 '22 at 10:24
8

You're probably including the file functions.php more than once.

codaddict
  • 445,704
  • 82
  • 492
  • 529
6

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.

Community
  • 1
  • 1
Muhammed Aslam C
  • 847
  • 1
  • 8
  • 14
4

I had strange behavor when my *.php.bak (which automaticly was created by notepad) was included in compilation. After I removed all *.php.bak from folder this error was gone. Maybe this will be helpful for someone.

Roman
  • 41
  • 1
4

Another possible reason for getting that error is that your function has the same name as another PHP built-in function. For example,

function checkdate($date){
   $now=strtotime(date('Y-m-d H:i:s'));
   $tenYearsAgo=strtotime("-10 years", $now);
   $dateToCheck=strtotime($date);
   return ($tenYearsAgo > $dateToCheck) ? false : true;
}
echo checkdate('2016-05-12');

where the checkdate function already exists in PHP.

3

I would like to add my 2 cent experience that might be helpful for many of you.

If you declare a function inside a loop (for, foreach, while), you will face this error message.

Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49
2

I don't like function_exists('fun_name') because it relies on the function name being turned into a string, plus, you have to name it twice. Could easily break with refactoring.

Declare your function as a lambda expression (I haven't seen this solution mentioned):

$generate_salt = function()
{
    ...
};

And use thusly:

$salt = $generate_salt();

Then, at re-execution of said PHP code, the function simply overwrites the previous declaration.

Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23
1

I'd recommend using get_included_files - as Pascal says you're either looking at the wrong file somehow or this function is already defined in a file that's been included.

require_once is also useful if the file you're attempting to include is essential.

John Parker
  • 54,048
  • 11
  • 129
  • 129
0

Since the code you've provided does not explicitly include anything, either it is being incldued twice, or (if the script is the entry point for the code) there must be a auto-prepend set up in the webserver config / php.ini or alternatively you've got a really obscure extension loaded which defines the function.

user229044
  • 232,980
  • 40
  • 330
  • 338
symcbean
  • 47,736
  • 6
  • 59
  • 94
0

means you have already created a class with same name.

For Example:

class ExampleReDeclare {}

// some code here

class ExampleReDeclare {}

That second ExampleReDeclare throw the error.

Naitik Shah
  • 513
  • 6
  • 13
0

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.

Tristanisginger
  • 2,181
  • 4
  • 28
  • 41
0

I had this pop up recently where a function was being called prior to its definition in the same file, and it didnt have the returned value assigned to a variable. Adding a var for the return value to be assigned to made the error go away.

Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
0

You have to deactivate the lite version in order to run the PRO version.

happymacarts
  • 2,547
  • 1
  • 25
  • 33
Serena
  • 1
  • I believe if you had any idea as to why this work. i would improve everyone's understanding of your solution. – Paulo Aug 25 '21 at 22:27
0

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')

Youssef
  • 2,866
  • 1
  • 24
  • 20
-1

or you can't create function in loop

  • such as

    for($i=1; $i<5; $i++) { function foo() { echo 'something'; } }

foo();
//It will show error regarding redeclaration

Ruthe
  • 363
  • 4
  • 9
-1

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.

Ameer Hamza
  • 108
  • 13