Reputation: 1025
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?
Upvotes: 73
Views: 216896
Reputation: 4435
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.
Upvotes: 4
Reputation: 5169
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.
Upvotes: 14
Reputation: 1
You have to deactivate the lite version in order to run the PRO version.
Upvotes: 0
Reputation: 8903
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.
Upvotes: 0
Reputation: 56351
Don't declare function inside a loop (like foreach
, for
, while
...) ! Declare before them.
You should include that file (wherein that function exists) only once. So,
instead of : include ("functions.php");
use: include_once("functions.php");
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() {
........
}
}
Upvotes: 90
Reputation: 2683
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.
Upvotes: 0
Reputation: 363
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
Upvotes: -1
Reputation: 1126
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.
Upvotes: 3
Reputation: 41
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.
Upvotes: 4
Reputation: 887
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.
Upvotes: 7
Reputation: 108
This errors says your function is already defined ; which can mean :
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.
Upvotes: -1
Reputation: 513
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.
Upvotes: 0
Reputation: 41
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.
Upvotes: 4
Reputation: 48357
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.
Upvotes: 0
Reputation: 3114
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')
Upvotes: 0
Reputation: 54425
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.
Upvotes: 1
Reputation: 454960
You're probably including the file functions.php more than once.
Upvotes: 8
Reputation: 400932
This errors says your function is already defined ; which can mean :
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.
Upvotes: 125