Click to See Complete Forum and Search --> : [RESOLVED] cannot redeclare error
jimot
Aug 4th, 2007, 08:01 PM
hi.. I have a function like this (taken from adsrotator plugin for wordpress):
<?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:
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
dclamp
Aug 5th, 2007, 01:08 PM
you may have included it twice. like this:
includes("function.php");
//....
includes("function.php");
or like this:
includes("function.php");
//...
function gatad() {
//...
}
penagate
Aug 5th, 2007, 02:23 PM
Use include_once or require_once to prevent such occurences
jimot
Aug 5th, 2007, 06:07 PM
you may have included it twice. like this:
includes("function.php");
//....
includes("function.php");
or like this:
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
dclamp
Aug 5th, 2007, 06:41 PM
have you tried removing the extra include?
jimot
Aug 5th, 2007, 06:49 PM
yes, if I use the include only once, that's fine, the one that gives the error is the second include..
dclamp
Aug 5th, 2007, 06:59 PM
yeah, you dont need to include it more then once.
jimot
Aug 5th, 2007, 07:34 PM
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
dclamp
Aug 5th, 2007, 07:36 PM
can you post your code? because you have confused me.
penagate
Aug 5th, 2007, 09:42 PM
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.
jimot
Aug 16th, 2007, 10:25 PM
it appears that the error comes from the wordpress plugin ... :blush: it's been solved.. thank you all for helping me :thumb:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.