"Fatal error: Cannot redeclare <function>"

PhpInclude

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

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.

Solution 6 - Php

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.

Solution 7 - Php

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.

Solution 8 - Php

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.

Solution 9 - Php

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.

Solution 10 - Php

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.

Solution 11 - Php

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.

Solution 12 - Php

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

Solution 13 - Php

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.

Solution 14 - Php

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.

Solution 15 - Php

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.

Solution 16 - Php

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

Solution 17 - Php

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

Solution 18 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfishmanView Question on Stackoverflow
Solution 1 - PhpPascal MARTINView Answer on Stackoverflow
Solution 2 - PhpT.ToduaView Answer on Stackoverflow
Solution 3 - PhpcodaddictView Answer on Stackoverflow
Solution 4 - Phpahmed hamdyView Answer on Stackoverflow
Solution 5 - PhpMuhammed Aslam CView Answer on Stackoverflow
Solution 6 - PhpRomanView Answer on Stackoverflow
Solution 7 - PhpPawel GlowackiView Answer on Stackoverflow
Solution 8 - PhpJacob BruinsmaView Answer on Stackoverflow
Solution 9 - PhpAhmad SharifView Answer on Stackoverflow
Solution 10 - PhpJohn ParkerView Answer on Stackoverflow
Solution 11 - PhpsymcbeanView Answer on Stackoverflow
Solution 12 - PhpYoussefView Answer on Stackoverflow
Solution 13 - PhpNaitik ShahView Answer on Stackoverflow
Solution 14 - PhpTristanisgingerView Answer on Stackoverflow
Solution 15 - PhpBrady MoritzView Answer on Stackoverflow
Solution 16 - PhpSerenaView Answer on Stackoverflow
Solution 17 - PhpRutheView Answer on Stackoverflow
Solution 18 - PhpAmeer HamzaView Answer on Stackoverflow