title: pass variable from function to loop and echo
body: <p>I want to loop the calcSLS function and at the right moment break and echo the printing_costs variable.</p>
  
  <p>When trying to do that I get undefined variable printing_costs.</p>
  
  <p>I know these variables are not available outside of a function or a loop but how can I transfer between those and do a proper echo of the result (=printing_costs).</p>
  
  <pre><code>&lt;?php
  
  $product_info['model']['box_volume'] = 100;
  $printer['price'] = 10;
  $product_info['model']['x_dim'] = 10;
  $product_info['model']['y_dim'] = 10;
  $product_info['model']['z_dim'] = 10;
  
  //printing_cost=product_info['model']['weight']*printer.data('price')*1000000000000;
  // added dimensions
  
  // standaard dimensies zijn in cm, converteren naar mm
  $x = $product_info['model']['x_dim'] * 100;
  $y = $product_info['model']['y_dim'] * 100;
  $z = $product_info['model']['z_dim'] * 100;
  $xyz = $x * $y * $z;
  
  function calcSLS($multiplier) {
    if ($z * $z&lt; $x * $y) {
        return (3.14 + ( 0.0023 * $x * $z))+ ( (0.042 * (( $x*$y)/($x*10))) * (z-1)) * $multiplier;
    } else
    if ($x &lt; $z) {
        return (3.14 + ( 0.0023 * $x * $z))+ ( (0.042 * (( $z*$y)/($z*10))) * ($x-1)) * $multiplier;
    } else
    {
        return (3.14 + ( 0.0023 * $x * $y))+ ( (0.042 * (( $x*$z)/($x*10))) * ($y-1)) * $multiplier;
    }
  };
  
  
  //if smaller than x use following multiplier
  //use . for a comma
  //use : to split x and multiplier
  //use ; to define end of multiplier and start a new comparison
  $multiplierString = "
  1001:-0.3;
  8001:0.3;
  27001:0.5;
  64001:0.62;
  125001:0.8;
  216001:0.9;
  343001:1.05;
  512001:1.2;
  729001:1.32;
  1000001:1.47;
  1331001:1.75;
  2197001:1.9;
  2744001:2.05;
  3375001:2.15;
  4096001:2.3;
  4913001:2.45;
  5832001:2.6;
  6859001:2.75;
  8000001:2.95;
  15625001:4;
  120000001:5;
  ";
  
  $multiplierArray = explode(";",$multiplierString);
  for ($i = 0; $i &lt; count($multiplierArray); $i++) {
    $helper = explode(":",$multiplierArray[$i]);
    if ($xyz &lt; $helper[0]) {
      $printing_cost = calcSLS($helper[1]);
      break;
    }
  }
  
  echo $printing_cost;
  
  ?&gt;
  </code></pre>