[wp-hackers] odd behaviour vs. global variables

Peter Westwood peter.westwood at ftwr.co.uk
Fri Jun 13 16:50:13 GMT 2008


Matthias Vandermaesen wrote:
> I was working on my plugin for Mollom (http://www.netsensei.nl/mollom) when
> I started noticing some odd behaviour when declaring certain variables
> global. For instance, If we create this simple plugin: (omitted the
> obligatory header for brevity's sake)
>
> 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.
>   
The global $do_comment line brings the global $do_comment variable into 
local scope.

This overrides the $do_comment variable passed into the function.

If you the global variable and the function argument differently then 
both will be available.

Cheers

westi

-- 
Peter Westwood
http://blog.ftwr.co.uk | http://westi.wordpress.com
C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5 



More information about the wp-hackers mailing list