Variable Scope, Or: How I Learned To Stop Worrying And Love The Globals.

I put this up in one of the sajax forums a bit earlier, but thought I’d post it here as well. When I was first working/learning/playing around with sajax (after my failed attempt at writing the ajax by hand (i’ll try again later)) I tried including the sajax in a page that was included in the main body from within a function, but couldn’t get it to work.

I looked and looked, and as far as I could tell, everything was setup correctly, and so on.

Going back and looking at it tonight, I had a bit of an epiphany. Saying ‘Global’ in the comment, doesn’t actually make the variables global. Shocking. I know.

In short I somehow mentally mushed the following block into a global declaration for these variables.

/*
* GLOBALS AND DEFAULTS
*
*/
$sajax_debug_mode = 0;
$sajax_export_list = array();
$sajax_request_type = "GET";
$sajax_remote_uri = "";

The trivial solution is of course to put said global declaration after the initial comment block, and then your off to the races.

One Response to Variable Scope, Or: How I Learned To Stop Worrying And Love The Globals.
  1. Ted
    April 1, 2005 | 12:29 pm

    Linkaroo – A site based almost entirely on Ajax. http://www.quixote.at.preempted.net/linkaroo/