|
-
Aug 4th, 2007, 08:01 PM
#1
Thread Starter
Addicted Member
[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
-
Aug 5th, 2007, 01:08 PM
#2
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
-
Aug 5th, 2007, 02:23 PM
#3
Re: cannot redeclare error
Use include_once or require_once to prevent such occurences
-
Aug 5th, 2007, 06:07 PM
#4
Thread Starter
Addicted Member
Re: cannot redeclare error
 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
-
Aug 5th, 2007, 06:41 PM
#5
Re: cannot redeclare error
have you tried removing the extra include?
My usual boring signature: Something
-
Aug 5th, 2007, 06:49 PM
#6
Thread Starter
Addicted Member
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..
-
Aug 5th, 2007, 06:59 PM
#7
Re: cannot redeclare error
yeah, you dont need to include it more then once.
My usual boring signature: Something
-
Aug 5th, 2007, 07:34 PM
#8
Thread Starter
Addicted Member
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
-
Aug 5th, 2007, 07:36 PM
#9
Re: cannot redeclare error
can you post your code? because you have confused me.
My usual boring signature: Something
-
Aug 5th, 2007, 09:42 PM
#10
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.
-
Aug 16th, 2007, 10:25 PM
#11
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|