title: php > performance > session vs globals vs db >?
body: <p>When comparing the following scenarios, which one would be the fastest? Sample examplary code below. </p>
  
  <p>Basically, what I'm trying to find out is what's the best alternative to using global vars.
  The global vars that I store would be the same for all users... they are application level values which are the same for all users, application_name for example, or application_home_page_url... </p>
  
  <p>I will have a ton of functions using these app level values and I do not want to pass them by argument, though I think that would be the fastest way.. My options seem to be working with either db look ups, or using session vars or globals..  </p>
  
  <p>Of course, you may take it to the next level by throwing the assoc. arrays into the equation to make it faster/?slower. </p>
  
  <p>The other factor that we have to keep in mind is that we are talking about lots of users here. </p>
  
  <p>So, what's the best way to toss around/work with these app level variables within individual functions? </p>
  
  <pre><code>//for global based approach
  $my_global_var1 = "abc";
  $my_global_var2 = "xyz";
  
  //for global + array based approach
  $my_globals['var1'] = "abc";
  $my_globals['var2'] = "xyz";
  
  //for session based approach
  $_SESSION['my_global_var1'] = "abc"
  $_SESSION['my_global_var2'] = "xyz";
  
  //for session + array based approach 
  $_SESSION['my_globals']['var1'] = "abc";
  $_SESSION['my_globals']['var2'] = "xyz";
  
  //for db based approach
  $varname = 'var1';
  
  //or some other way, you may think of
  //??
  
  function func1()
  {
     global $my_global_var1;
     global $my_global_var2;
     //....
  
      my_var1 = $my_global_var1;
  }
  
  
  function func2()
  {
      $my_var1 = $my_globals['var1'];
  }
  
  function func3()
  {
      $my_var1 = $_SESSION['my_global_var1'];
  }
  
  function func4()
  {
      $my_var1 = $_SESSION['my_globals']['var1'];
  }
  
  $my_var1 = func5($varname)
  function func5($varname)
  {
      return lookup($varname);
  }
  </code></pre>