Php function already declared here

Hi there,

i have a php function error which is

Fatal error: Cannot redeclare randalphanum() (previously declared in /home/willbcco/public_html/bccms/functions.php:104) in /home/willbcco/public_html/bccms/functions.php on line 112

Now line 104 and line 112 is the whole function line 104 is


 function randAlphaNum($n)

and which this is what the function contains


function randAlphaNum($n)
{
	$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	$randstr = "";
	$max = strlen($str)-1;
	for($i = 0; $i<$n; $i++)
	$randstr .= $str[mt_rand(0,$max)];
	return $randstr;
}

line 112 is the end of the function i cant understand whats causing that error to show up?

Why do i get that error?

Is it a possibility that the file has already been executed once before it comes to this error?

This error is caused when the function is declared more than once. I don’t see anything in the code you provided that would cause this error. Look to see if you have declared the function anywhere else on the page. This includes declarations that are part of a script pulled in through include() or require(). If the same script is included twice it will also cause this error.

Try inserting this code and it should test for the function before creating.

If it solves your problem the you can move on and return to sort this one out at a later date :slight_smile:


  
  if (function_exists('randAlphaNum')) 
  {
    // echo "<br /> function: randAlphaNum(...)  already exists.<br />\
";
    // die;
}else{
  function randAlphaNum($n) 
  { 
    $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
    $randstr = ""; 
    $max = strlen($str)-1; 
    for($i = 0; $i<$n; $i++) 
    $randstr .= $str[mt_rand(0,$max)]; 
    return $randstr; 
  }   
}
?> 


.

Using a text editor which allows multiple files to be searched at the same time search all the files in the folders for the app concerned for “function randAlphaNum”

It’s obvious from the error that the redeclaration is taking place at exactly the same point in the code as the original function declaration.

So the functions.php file is definitely being included more than once.

i’d this experience too when i included others classes, modules, snippets into my custom built script which has repeated function name with them.

so i just use http://www.searchreplacetext.com/index.html and search for the function name whole folder containing every scripts.

example in your case, search “function randAlphaNum”, and find which file contains the string that is also included into your functions.php file.

okay thanks mate its fixed

Can you let us in on the secret so that it will help anyone else with a similar problem?

.

sure it was simply removing the include_once on the file and it was fixed

1 Like