title: How can I get a value from inside of function?
body: <p>Is it possible to get values from inside a function and use those outside of the function?</p>
  <p>Here is my code:</p>
  <pre><code>&lt;?php
  function cart() {
    foreach($_SESSION as $name =&gt; $value){
      if ($value&gt;0) {
        if (substr($name, 0, 5)=='cart_') {
          $id = substr($name, 5, (strlen($name)-5));
          $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
  
          while ($get_row = mysql_fetch_assoc($get)) {
            $sub = $get_row['price']*$value;
            echo $get_row['name'].' x '.$value.' @ &amp;pound;'.number_format($get_row['price'], 2).' = &amp;pound;'.number_format($sub, 2).'&lt;a href=&quot;cart.php?remove='.$id.'&quot;&gt;[-]&lt;/a&gt; &lt;a href=&quot;cart.php?add='.$id.'&quot;&gt;[+]&lt;/a&gt; &lt;a href=&quot;cart.php?delete='.$id.'&quot;&gt;[Delete]&lt;/a&gt;&lt;br /&gt;';
          }
        }      
        $total += $sub ;
      }
    }
  }
  ?&gt;
  </code></pre>
  <p>Now my question is how can I get the value of <code>$total</code>?  I want to use that value outside of the function. I have 2 functions 1 cart and 1 discount.</p>
  <p>I tried <code>return $total;</code> (inside of function)</p>
  <p>For example</p>
  <pre><code>$final = cart () - discount ();
  echo $final;
  </code></pre>
  <p>It echos out both function, but code which is inside of function doesn't do any mathematical operation.</p>