1

After running a drush update on my drupal 7 site, the php code that I use to bring in user information for a form executes twice. This is a problem because I have created some functions in the php that get called so when it executes the PHP the second time it tries to re-declare the functions and I get errors like this:

PHP Fatal error:  Cannot redeclare fooBar() (previously declared in [path_to_drupal7]/modules/php/php.module(80) : eval()'d code:3) in [path_to_drupal7]/modules/php/php.module(80) : eval()'d code on line 4

It doesn't matter what the function is called or what it does. In this example case this is the code:

<?php
function fooBar() {
    print "foo bar";
}

fooBar();
?>

It also doesn't seem to matter what content type the page is (I my case I need it to bring user information into a form).

Why is Drupal executing the PHP twice? And more importantly, how can I keep it from doing so?

EDIT: Drupal seems to be executing the php once for the trimmed version and once for full version and once for the full version. This is what I expect when I preview the post. I don't really care about the preview version so I would be happy to get rid of it. Why is Drupal executing the code twice when I view the page? (why is it running it for the trimmed version when I'm actually viewing the page?)

2
  • What modules were updated? Commented Sep 16, 2013 at 15:57
  • It looks like the drupal core was updated from 7.22 to 7.23. The Views module was also updated. Commented Sep 17, 2013 at 13:46

2 Answers 2

2

Something must be re-including/requiring the file containing this function or you have this function within a loop. Find that, or, wrap it with function_exists.

<?php

if (!function_exists('fooBar')) {
    function fooBar() {
        print "foo bar";
    }
}

fooBar();
Sign up to request clarification or add additional context in comments.

3 Comments

that works as a way to get things working for now but it doesn't really solve the problem. Drupal is still running the code twice, and that's really what I want to prevent. Also, the fooBar() needs to be moved inside the if statement otherwise it will get called twice as well.
@cjc, can you provide a little more information? What modules are you using? Where is this function being called? What should this function do?
The code is being executed as the body of the page using PHP code. Turns out the the fooBar() doesn't need to be inside the if statement since the first execution is for the trimmed text and the second execution is for the full text.
0

Like you said, the php you entered as input is being eval'd twice....once for the teaser, and once for the full body text.

Best practice: Don't define functions with eval. It can get messy fast.

Better practice: eval = evil. Don't use it.

In the time it takes to debug the problem, you could have written a very basic module that defines fooBar, then you can execute it from your PHP input field if you want.

Comments

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.