Using Static Variables in a Custom Module I'm trying to define a static variable for use in my custom module. I want it to let me pass a number from function to function. I'm using drupal_static() but I can't get it to work. How do I do this?

This is the code I'm using to define my static variable.

$var = &amp;drupal_static(__FUNCTION__);
if (!isset($var)) {
  // generate contents of static variable
  $var = '8';
}
return $var;


I've tried putting it in a custom module. 

 function viewusercontact_locateuser($value, $key, &amp;$counter) {
   global $user;

   /**********In here***********/

   if ($value['uid'] == $user-&gt;uid) {
      ........
   }
}


I can't seem to get it to work. What am I doing wrong?

(I got my code from the following guides)

https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_static/7
http://www.lullabot.com/blog/article/beginners-guide-caching-data-drupal-7
http://erikwebb.net/blog/drupal-design-patterns/static
drupal_static() stores variables based on the name of the function they're related to (hence the use of __FUNCTION__ when you initialise it in the relevant function).

You can pick it up again in any other function by querying drupal_static() again, with the name of the originating function. So in your case:

function viewusercontact_foo() {
  $static_var = drupal_static('viewusercontact_locateuser');
  ...
}