title: How to evaluate formula passed as string in PHP?
body: <p>Just trying to figure out the proper and safer way to execute mathematical operation passed as string. In my scenario it is values fetched from image EXIF data.</p>
  
  <p>After little research I found two way of doing it.</p>
  
  <p>first, using <code>eval</code>:</p>
  
  <pre><code>function calculator1($str){
      eval("\$str = $str;");
      return $str;
  }
  </code></pre>
  
  <p>second, using <code>create_function</code>:</p>
  
  <pre><code>function calculator2($str){
      $fn = create_function("", "return ({$str});" );
      return $fn();
  };
  </code></pre>
  
  <p>Both examples require string cleanup to avoid malicious code execution. Is there any other or shorter way of doing so?</p>