在PHP中实现自定义数学表达式解析器和评估器
答案是:通过分步实现词法分析、Shunting Yard算法解析和RPN求值,可构建安全可控的PHP数学表达式求值器。1. tokenize函数将输入拆分为数字、变量、操作符等标记;2. parseToRPN使用Shunting Yard算法按优先级和结合性转换为逆波兰表示;3. evaluateRPN利用栈结构结合变量上下文计算结果;4. evaluateExpression整合流程并处理异常;5. 示例显示支持变量和标准运算,具备安全性、可扩展性与错误处理能力,适用于需避免eval()风险的场景。
Creating a custom mathematical expression parser and evaluator in PHP can be useful when you need more control than eval()
provides—especially for security, extensibility, or educational purposes. While eval()
can execute expressions, it's dangerous with untrusted input and lacks flexibility. Building your own parser allows you to safely evaluate expressions like 2 + 3 * (4 - 1)
while supporting variables, functions, and error handling.

Here’s how to implement a simple yet effective mathematical expression parser and evaluator in PHP.
1. Tokenization: Breaking the Input into Meaningful Parts
The first step is to convert the input string into tokens—numbers, operators, parentheses, variables, and functions.

function tokenize($expression) { $tokens = []; $length = strlen($expression); $i = 0; while ($i < $length) { $char = $expression[$i]; if (ctype_digit($char) || $char === '.') { $num = ''; while ($i < $length && (ctype_digit($expression[$i]) || $expression[$i] === '.')) { $num .= $expression[$i++]; } $tokens[] = ['type' => 'number', 'value' => (float)$num]; $i--; } elseif (ctype_alpha($char)) { $var = ''; while ($i < $length && ctype_alnum($expression[$i])) { $var .= $expression[$i++]; } $tokens[] = ['type' => 'variable', 'value' => $var]; $i--; } elseif (in_array($char, ['+', '-', '*', '/', '^', '(', ')'])) { $tokens[] = ['type' => 'operator', 'value' => $char]; } elseif ($char === ' ') { // Skip whitespace } else { throw new Exception("Unknown character: $char"); } $i++; } return $tokens; }
This tokenizer handles numbers (including decimals), variables (like x
, y1
), operators, and parentheses.
2. Parsing with Shunting Yard Algorithm (Convert to RPN)
We’ll use the Shunting Yard algorithm by Edsger Dijkstra to convert infix notation (standard math) to Reverse Polish Notation (RPN), which is easier to evaluate.

function parseToRPN($tokens) { $output = []; $operators = []; $precedence = ['+' => 1, '-' => 1, '*' => 2, '/' => 2, '^' => 3]; $associativity = ['+' => 'left', '-' => 'left', '*' => 'left', '/' => 'left', '^' => 'right']; foreach ($tokens as $token) { if ($token['type'] === 'number' || $token['type'] === 'variable') { $output[] = $token; } elseif ($token['value'] === '(') { $operators[] = $token; } elseif ($token['value'] === ')') { while (!empty($operators) && end($operators)['value'] !== '(') { $output[] = array_pop($operators); } array_pop($operators); // Remove '(' } elseif ($token['type'] === 'operator') { $op = $token['value']; while ( !empty($operators) && ($top = end($operators))['value'] !== '(' && isset($precedence[$top['value']]) && ( ($associativity[$op] === 'left' && $precedence[$op] <= $precedence[$top['value']]) || ($associativity[$op] === 'right' && $precedence[$op] < $precedence[$top['value']]) ) ) { $output[] = array_pop($operators); } $operators[] = $token; } } while (!empty($operators)) { $output[] = array_pop($operators); } return $output; }
This algorithm respects operator precedence and associativity (e.g., ^
is right-associative).
3. Evaluating the RPN Expression
Now evaluate the RPN using a stack. Variables are resolved from a provided context (e.g., ['x' => 5]
).
function evaluateRPN($rpn, $variables = []) { $stack = []; foreach ($rpn as $token) { if ($token['type'] === 'number') { $stack[] = $token['value']; } elseif ($token['type'] === 'variable') { if (!isset($variables[$token['value']])) { throw new Exception("Undefined variable: " . $token['value']); } $stack[] = $variables[$token['value']]; } elseif ($token['type'] === 'operator') { if (count($stack) < 2) { throw new Exception("Invalid expression"); } $b = array_pop($stack); $a = array_pop($stack); switch ($token['value']) { case '+': $stack[] = $a + $b; break; case '-': $stack[] = $a - $b; break; case '*': $stack[] = $a * $b; break; case '/': if ($b == 0) throw new Exception("Division by zero"); $stack[] = $a / $b; break; case '^': $stack[] = pow($a, $b); break; default: throw new Exception("Unknown operator: " . $token['value']); } } } if (count($stack) !== 1) { throw new Exception("Invalid expression"); } return $stack[0]; }
4. Putting It All Together
Now create a simple wrapper function:
function evaluateExpression($expression, $variables = []) { try { $tokens = tokenize($expression); $rpn = parseToRPN($tokens); return evaluateRPN($rpn, $variables); } catch (Exception $e) { return "Error: " . $e->getMessage(); } }
5. Usage Example
echo evaluateExpression("2 + 3 * (4 - 1)"); // Output: 11 echo evaluateExpression("x + y * 2", ['x' => 5, 'y' => 3]); // Output: 11 echo evaluateExpression("z ^ 2", ['z' => 4]); // Output: 16
Key Features & Benefits
-
Safe: No use of
eval()
, so no code injection. -
Extensible: You can add functions (e.g.,
sin
,sqrt
) by extending the tokenizer and evaluator. - Error Handling: Clear messages for undefined variables, syntax issues, or division by zero.
- Supports Variables: Useful for dynamic expressions in forms, calculators, or rule engines.
Possible Enhancements
- Support for built-in math functions (e.g.,
sin(x)
). - Unary operators (e.g.,
-5
,+3
). - Better error recovery and syntax validation.
- Custom operator definitions.
Building your own parser gives deep insight into how programming languages and calculators work. While tools like eval()
or external libraries exist, a custom parser offers control, safety, and learning value.
Basically, it's not complex once you break it down—tokenize, parse, evaluate.
以上是在PHP中实现自定义数学表达式解析器和评估器的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

浮点数不精确是PHP中常见问题,答案在于其使用IEEE754双精度格式导致十进制小数无法精确表示;1.0.1或0.2等数在二进制中为无限循环小数,计算机需截断造成误差;2.比较浮点数时应使用容差而非==,如abs($a-$b)

round()uses"roundhalftoeven",not"roundhalfup",soround(2.5)returns2andround(3.5)returns4tominimizestatisticalbias,whichmaysurprisethoseexpectingtraditionalrounding.2.Floating-pointrepresentationerrorscausenumberslike2.675tobestored

bcmathisesene forAccratecryptoCurrencyCalcalsionSinphpBecausefloing-pointarithmeticIntroducesunAcceptablebablerOundingErrors.1.floation-pointnumberslike0.1 0.2yieldimimpreciseresults(e.g.,e.g.,0.30000000000000000000004)

AvectorinPHPgraphicsrepresentsposition,direction,orvelocityusingaclasslikeVector3Dwithx,y,zcomponents.2.Basicoperationsincludeaddition,subtraction,scalarmultiplication,anddivisionformovementandscaling.3.MagnitudeiscalculatedviathePythagoreantheorem,a

要提升PHP中的进制转换能力,首先需实现自定义进制转换函数以支持超过36的进制和自定义字符集,1.使用toBase和fromBase函数结合自定义digits数组可实现任意进制转换;2.处理大数时应使用BCMath扩展的bccomp、bcmod和bcdiv函数确保精度;3.构建BaseEncoder类实现双向安全映射,确保编码解码可逆;4.始终验证输入并统一字符顺序;5.避免使用base_convert处理大数,优先选择GMP提升性能,最终实现健壮、可扩展的进制转换系统。

计算平均值:使用array_sum()除以元素个数得到均值;2.计算中位数:排序后取中间值,偶数个元素时取中间两个数的平均值;3.计算标准差:先求均值,再计算每个值与均值差的平方的平均数(样本用n-1),最后取平方根;通过封装这三个函数可构建基础统计工具类,适用于中小规模数据的分析,且需注意处理空数组和非数值输入,最终实现无需依赖外部库即可获得数据的核心统计特征。

ModularArithMeticisessentialInphPcryptographlicationsdeSpitePhpnotBeingAhigh-Performancelanguage; 2. ItunderPinspublic-keysystemsslikersaanddiffie-hellmanthranthroughoperationssuchasmodularexpormentiationAndirestiationAndIrverses; 3.php'snative; 3.php'snative; 3.php'snative;

GMPisessentialforhandlinglargeintegersinPHPbeyondnativelimits.1.GMPenablesarbitrary-precisionintegerarithmeticusingoptimizedClibraries,unlikenativeintegersthatoverfloworBCMaththatisslowerandstring-based.2.UseGMPforheavyintegeroperationslikefactorials
