Results 1 to 11 of 11

Thread: [RESOLVED] cannot redeclare error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Resolved [RESOLVED] cannot redeclare error

    hi.. I have a function like this (taken from adsrotator plugin for wordpress):

    Code:
    <?php
    function getads($ad = '') {
      $ad = trim($ad);
      if(strlen($ad) > 0) {
        $ad = ABSPATH . "ads/" . $ad . '.txt';
        if(file_exists($ad)) {
          $ads = file($ad);
          return $ads[rand(0, sizeof($ads)-1)];
        }
      }
    }
    
    $adds=getads('ad_160x600');
    include ABSPATH . "ads/" . trim($adds);
    ?>
    now when I try to use the function again it always give me this error:
    Code:
    Cannot redeclare getads() (previously declared in......
    from what I read on php.net, it's because of the include function. How can I fix this?

    Thanks

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: cannot redeclare error

    you may have included it twice. like this:

    PHP Code:
    includes("function.php");
    //....
    includes("function.php"); 
    or like this:
    PHP Code:
    includes("function.php");
    //...
    function gatad() {
    //...

    My usual boring signature: Something

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: cannot redeclare error

    Use include_once or require_once to prevent such occurences

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: cannot redeclare error

    Quote Originally Posted by dclamp
    you may have included it twice. like this:

    PHP Code:
    includes("function.php");
    //....
    includes("function.php"); 
    or like this:
    PHP Code:
    includes("function.php");
    //...
    function gatad() {
    //...

    yes I did the include twice.. how can I overcome this?

    Use include_once or require_once to prevent such occurences
    I tried include_once and require_once but still gives the same error.

    thanks

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: cannot redeclare error

    have you tried removing the extra include?
    My usual boring signature: Something

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: cannot redeclare error

    yes, if I use the include only once, that's fine, the one that gives the error is the second include..

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: cannot redeclare error

    yeah, you dont need to include it more then once.
    My usual boring signature: Something

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: cannot redeclare error

    the problem is I need to include the result of the function..

    this is what the function does:
    it takes one line randomly from a text file, in which each line in the text file is a URL.

    Then, I need to include the URL to my site, and I need to do that more than once. That's why I use include more than once.

    thanks

  9. #9
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: cannot redeclare error

    can you post your code? because you have confused me.
    My usual boring signature: Something

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: cannot redeclare error

    Use require_once to load the code file containing the function and simply call the function whenever it is required. Your code—as posted—does not call the function at all, it merely includes it.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    237

    Re: cannot redeclare error

    it appears that the error comes from the wordpress plugin ... it's been solved.. thank you all for helping me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width