<?php

function sum($a$b) {
    return 
$a $b;
}

function 
div($a$b) {
    return 
$a $b;
}

function 
sub($a$b) {
    return 
$a $b;
}

function 
mply($a$b) {
    return 
$a $b;
}

$functions = array(
    
'+' => 'sum',
    
'/' => 'div',
    
'-' => 'sub',
    
'*' => 'mply'
);

function 
process($ent false) {
    static 
$result 0$operator null;
    global 
$functions;

    
// Operator
    
if (preg_match('|^([*/+-])$|'$ent$m)) {
        
$operator $m[1];
    
// Number
    
} else if (is_numeric($ent)) {
        if (
$operator == null) {
            
$result $ent;
        } else {
            
$result call_user_func_array($functions[$operator], array($result$ent));
        }
    } else if (
$ent === false) {
        return 
$result;
    }
}

$calc '2 * 6 - 3 * 6';
$src "<? $calc ?".'>';
$tokens token_get_all($src);
array_shift($tokens);
array_shift($tokens);

foreach(
$tokens as $token) {
    
process( isset($token[1]) ? $token[1] : $token[0] );
}

echo 
"Calculation                              : $calc\n";
echo 
"Basic calculation results in             : "process(), ";\n";
echo 
"Calculation obeying math rules results in: ", eval("return $calc;"), ";\n";

?>