[wp-hackers] odd behaviour vs. global variables

Austin Matzko if.website at gmail.com
Fri Jun 13 15:33:06 GMT 2008


On Thu, Jun 12, 2008 at 8:30 AM, Matthias Vandermaesen
<matthias.vandermaesen at gmail.com> wrote:
> function dosomething($do_comment) {
>    global $do_comment; // let's declare it global
>    print_r($do_comment);
>    return $do_comment; // or alternatively: die();
> }
> add_action('preprocess_comment', 'dosomething');
>
> The $do_comment variable seems to be non-existant or empty! Meaning
> everything else breaks from here on as the input got lost somewhere along
> the way. But when we remove the 'global $do_comment' line, everything works
> like a charm and $do_comment  passes the commentdata $correctly.

[snip]

> imho, this is just odd behaviour and I was curious if the list could give me
> some feedback or reasonable explanation on this one.

The declaration statement

global $do_comment;

is the same as saying

$do_comment = $GLOBALS['do_comment'];

so effectively you're re-defining $do_comment, losing the value that
was passed to the function.


More information about the wp-hackers mailing list