73

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?

18 Answers 18

126

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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 ?
This is the only file that has a generate_salt() function.
which means that it's probably being included more than once
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
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.
|
91

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() { ........ } } 

2 Comments

in my case i was creating a function inside a loop - my bad
Your answer pointed me in the right direction. My problem was that, though I wanted to declare a function in the global namespace, I was testing for its existence and declaring it in a specific namespace. So, we have to include the namespace of a function when we call function_exists and we should be careful where we are declaring that function.
15

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.

2 Comments

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.
this is a very good method
8

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

Comments

7

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.

3 Comments

What's wrong with including a function within a function?
this answer helped me
That was the explanation for my problem.
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.

Comments

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.

Comments

4

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.

Comments

3

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.

4 Comments

THIS IS EXCELLENT, you have absolutely made my day <3
@AlexHighHigh cool, it seemed so obvious but nobody said anything about it.
I'll elaborate on it's description at some point but for now I'm saving this in my lil magic library of useful snippets to come back to at a later date: freshlondon.biz/lambda-expression
This is fucxking awesome bruh!
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.

Comments

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

Comments

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.

Comments

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.

Comments

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.

Comments

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.

Comments

0

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

1 Comment

I believe if you had any idea as to why this work. i would improve everyone's understanding of your solution.
-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.

Comments

-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.