0

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?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
triplus
  • 91
  • 5
  • 17

3 Answers3

1

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

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • So you mean I can put all of the above code in the function in testmodify.php? The function is in the testmodify.php located. Am I right? – triplus Feb 20 '15 at 13:17
  • What's in your testmodify.php move in some function inside the same file. Then include testmodify.php at begining of you script you shared here and from the loop instead of including just call your script. – MilanG Feb 20 '15 at 13:21
  • Thank you for your help. Maybe its to easy and I don't get it. How do I call my script from the loop? My script has the name "testfile.php". The "show" definition is already a function in testmodify.php. I moved the include in testfile.php to the top of my script. Can you help me one more time? Thank you! – triplus Feb 20 '15 at 14:02
  • Sorry, I don't know what exactly is inside your testmodify.php. If all code there is already inside some function then just include it once at the top and call the function from the place where include was. But if you have some code outside of any function then create some function, place the code in it, include file at the top and call the function from the place where you include file now. – MilanG Feb 20 '15 at 14:07
  • Ok. I got it. Now it works very fine. Very much thanks for your support. I am not able to do a "Vote up". – triplus Feb 22 '15 at 17:00
0

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.

Community
  • 1
  • 1
Xeschylus
  • 148
  • 1
  • 7
0

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 :)

Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37
  • Thanks for your help. Sorry I didn't mention it before. The Definition of show() is already a function in the testmodify.php script. – triplus Feb 20 '15 at 14:23