title: How to create functions in PHP
body: <p>I'm new to PHP and I've created a PHP session script that times-out after 30 minutes of inactivity and it works fine.</p>
  
  <p>However, I would like to put it in a function and call it in pages that would require it in order to avoid DRY. How can I achieve this?</p>
  
  <p>PS: The function I created throws 'Undefined index' error.
  Snippet of the code is below:</p>
  
  <pre><code>$_SESSION['LAST_ACTIVITY'] = time();
      if (isset($_SESSION['LAST_ACTIVITY']) &amp;&amp; (time() - $_SESSION[$'LAST_ACTIVITY'] &gt; 5)) {
          // last request was more than 30 minutes ago
          session_unset();
          session_destroy();
          header('Location: index.php');
            // exit;              
      }
  </code></pre>