[wp-hackers] odd behaviour vs. global variables

Andy Skelton skeltoac at gmail.com
Fri Jun 13 14:13:22 GMT 2008


On Thu, Jun 12, 2008 at 7:30 AM, Matthias Vandermaesen
<matthias.vandermaesen at gmail.com> wrote:
> function dosomething($do_comment) {
>    global $do_comment; // let's declare it global

>From http://php.net/global :

"By declaring $a and $b global within the function, all references to
either variable will refer to the global version."

$do_comment is a local variable before the global keyword. After the
global keyword $do_comment refers to the uninitialized global. You
must use an assignment. The global keyword doesn't work the way you
assumed.

function dosomething($comment) {
 global $do_comment;
 $do_comment=$comment;

Cheers,
Andy


More information about the wp-hackers mailing list