triplus
triplus

Reputation: 91

Include PHP File and execute it multiple times

Yeah I know this is probably not the best way to perform this. However I have a script which I would like to call multiple times with parameters and hand over two variables. The code I have so far looks like this:

$testfeld = array('User1'=>'400028',
                  'User2'=>'400027'
);

foreach($testfeld as $key=>$value) {
    $_GET['cmd'] = 'modify';
    include("testmodify.php");
}

If I run this code, I get an error message:

Fatal error: Cannot redeclare show() (previously declared in testmodify.php:14) in testmodify.php on line 39

If I perform this, it only modifys me the first entry of the array and then throws me the error message above. Sadly, I have only read access to testmodify.php.... So is there a way to perform this include with all the content of the array?

Upvotes: 0

Views: 2715

Answers (3)

You can do something like (in your testmodify.php script):

if (!function_exists("show")) {
    function show($parameters) {
        /* do your stuff here */
    }
}

A better thing to do, in this case, would be to perform include_once() in your testmodify.php script, making it point to a script file where you define your show() function. My approach should also work for you.

Hope that helps :)

Upvotes: 0

Xeschylus
Xeschylus

Reputation: 148

You are receiving this error because the file testmodify.php contains the definition for "show". A second inclusion of the file caused by your loop attempts to define the object show again, which cannot be done.

Here is another SO question about someone trying to redefine a function. Php and redifining

Edit: the other answer by MilanG is how you could go about fixing it.

Upvotes: 0

MilanG
MilanG

Reputation: 7114

Put your code in the function, include it once and call as much as you want.

Upvotes: 1

Related Questions