You are here

Handle PHP & JavaScript Global Variables in Drupal

Declaring and accessing PHP and JavaScript globals is not as straight forward as I thought it would be. first of all I'm going to go over how I first tried to implement PHP and JavaScript global variables and then what actually worked for me in Drupal 5 and 6.

Handle PHP & JavaScript Global Variables in Drupal

The PHP documentation on global variables - can be found inside the Variable Scope section - implies that if a variable is used within the context of a global variable, then the scope of this variable will span to includes and required files.

Problem

The PHP Docs suggests that the following scenario is possible. I have a module which requires a file named example.php. This file has the folling code:

<?php
$my_global_var
= 'my value'; // global scope assignment

function doSomething() {
  global
$my_global_var;
  print
$my_global_var;
}
?>

The .module file has the following code:

<?php
 
require('example.php');
 
doSomething();
?>

Output

The doSomething() function will return no output

Solution

Now, if I add this before the global scope assignment in example.php the output is as expected.

global $my_global_var;

or, I could also use the following code inside the doSomething() function; which does work as expected based on the example given in the PHP Docs. Just replace the two lines inside the function with this one:

print $GLOBALS['example'];

Important! don't do this

global $my_global_var = 'my value';

I recommend reading the whole sub-section titled "The global keyboard" under the Variable Scope

Handle PHP & JavaScript Global Variables in Drupal

Creating global variables in JavaScript is much like creating global variables in PHP, with the exception that in JavaScript we could actually declare a global variable within a function by using the window object. So we could use either of the following examples:

Example one:

var my_global_var = "my value";
function action() {
  document.write(my_global_var);
}

Example two:

function action() {
  window.my_global_var = "another value";
}

function triggerAction() {
  alert(window.my_global_var);
}

Example three:

var my_global_var;
function action() {
  my_global_var = "yet another value";
}

function triggerAction() {
  document.write(window.my_global_var);
}

Problem

Yet, when I try to do this in Drupal it does NOT work. What does seem to work, is using one of Drupal's built-in PHP function calleddrupal_add_js().

Solution

Here's how I set a global JavaScript variable using this function:

drupal_add_js(array('my_global_var' =&gt; 'some value'), 'setting');

Here's how the Java global variable can be accessed.

alert(Drupal.settings.my_global_var);

Out of the many different functionalites that can be accomplish by using the drupal_add_js() function as explained in the Drupal API, using the parameter 'setting' adds the defined setting to Drupal's global storage of JavaScript settings and then can be accessible through Drupal.settings

Hopefully this blog will help someone out there determine why their PHP and JavaScript global variables don't work in Drupal, and hopefully someone can give their 2-cents as to why the PHP and JavaScript global variables behave differently in Drupal.

References:

  • Drupal Add JS - drupal_add_js()
  • PHP Variables

lifestyle: