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?

5
  • Does the file 'testmodify.php' define a function? If so then that is the cause of the error. Why not include the file once and refer to the function repeatedly? Commented Feb 20, 2015 at 13:04
  • Thats because you're redeclaring show(), instead of including it inside the foreach, include it above it, then run the function you want to run inside the loop... Commented Feb 20, 2015 at 13:05
  • You meant foreach($testfeld right? Commented Feb 20, 2015 at 13:06
  • Yeah sorry. I meant foreach($testfeld... My bad. Commented Feb 20, 2015 at 13:11
  • No problemo. I've made the appropriate edit. Commented Feb 20, 2015 at 13:14

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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!
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.
Ok. I got it. Now it works very fine. Very much thanks for your support. I am not able to do a "Vote up".
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.

Comments

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

1 Comment

Thanks for your help. Sorry I didn't mention it before. The Definition of show() is already a function in the testmodify.php script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.