Fatal error: Cannot redeclare function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Fatal error: Cannot redeclare function

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fatal error: Cannot redeclare function

Post by Celauran »

You're calling include inside the loop. Try include_once instead.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Fatal error: Cannot redeclare function

Post by donny »

still getting it
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fatal error: Cannot redeclare function

Post 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.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Fatal error: Cannot redeclare function

Post 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.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Fatal error: Cannot redeclare function

Post 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.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Fatal error: Cannot redeclare function

Post by donny »

these functions work fine when they're done with only 1 . but for some reason the loop is causing a problem.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Fatal error: Cannot redeclare function

Post 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.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Fatal error: Cannot redeclare function

Post by donny »

thank you i placed the functions outside of the loop and it works.

thanks a lot for all your help!
Post Reply