Page 1 of 1

Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:17 pm
by donny
hello,

i am having trouble with a function i made

Code: Select all

function randLetter()
{
    $int = rand(0,23);
    $a_z = "ABCDEFGHJKMNOPQRSTUVWXYZ";
    $rand_letter = $a_z[$int];
    return $rand_letter;
}
function randNumb()
{
    $int = rand(0,8);
    $a_z = "123456789";
    $rand_letter = $a_z[$int];
    return $rand_letter;
}
those are my functions.
i have them in a foreach loop. it works for the first one and then when it goes to do the 2nd one i get this error.

Fatal error: Cannot redeclare randLetter() (previously declared in script.php)

anybody got any suggestions on how i can make it work?

thank you

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:21 pm
by Celauran
You're calling include inside the loop. Try include_once instead.

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:26 pm
by donny
still getting it

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:31 pm
by Celauran
Do you have those functions defined in more than one place? Can't do that. You might want to move them to a separate file. Also make sure you don't have multiple functions with the same name.

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:35 pm
by donny
in my code i use them like this

Code: Select all

$boxnumb = randLetter() . randNumb() . $ordernumber . randNumb() . randLetter();
basically what i need to do is put a 1 random letter and 1 random number before and after $ordernumber.
if theres a way i can do this without using a function that'll be good so i can just eliminate the function completely.

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:41 pm
by donny
then i also have another function that might cause a similar problem which is this one

Code: Select all

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}

$fullnameedited = ucname("{$fullname}\n");
this edits their full name.. it basically makes the first letter of the first and last name a capital and the rest lowercase. it also capitalizes after ' and - if they have them in their names. this is just to make a neat report for myself .
if there is a way to do this without using it as a function and i can get rid of it i shouldn't have any further problems anymore.

any help would be greatly appreciated. thank you.

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 6:43 pm
by donny
these functions work fine when they're done with only 1 . but for some reason the loop is causing a problem.

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 7:40 pm
by Celauran
You can only declare any given function once. Because they're inside the file being included, you're effectively trying to declare them on each iteration of the loop. Place them somewhere else, include that new file once, and the problem goes away.

Re: Fatal error: Cannot redeclare function

Posted: Sat Sep 06, 2014 9:13 pm
by donny
thank you i placed the functions outside of the loop and it works.

thanks a lot for all your help!