title: Using a function to initialise a variable if it not set - PHP
body: <p>Upon executing a script, sometimes the variable will be set, and sometimes it won't. The times that it isn't, I'm given a notice that the variable is not defined.</p>
  
  <p>In efforts to clear the notice, I simple added the following code</p>
  
  <pre><code>if(!isset($var)) {
      $var = NULL;
  }
  </code></pre>
  
  <p>That works just as needed because it tests if the variable isn't already set so that we don't set something that we need to <em>NULL</em>. But in a file where there are over 60 variables that are of this case and more to come, I thought creating a simple function to do so would be easier. So I started with this:</p>
  
  <pre><code>function init($var) {
      if(!isset($var)) {
          return $var = NULL;
      }
  }
  </code></pre>
  
  <p>Obviously that doesn't work and is also riddled with errors that will annoy most programmers out there (such as the <code>!isset()</code> inside a function, not supplying a return statement in case the if statement is false, etc.) but that's just to give you the basic jist of what I need so in the code I can just call <code>init($var);</code> to test if the variable isn't already set, and then creates one and sets it to <em>NULL</em> to avoid the notice.</p>
  
  <p>Is this even possible? To use a function to test if a variable is already set outside of the function? Thanks in advance :)</p>