cognito/evalmath

评估数学和逻辑方程

1.1.2 2019-02-15 01:24 UTC

This package is auto-updated.

Last update: 2024-09-15 13:49:17 UTC


README

PHP EvalMath与逻辑方程

Build Status

本包通过Composer添加Miles Kaufmann的EvalMath包。有关原始包的更多信息,请访问 https://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html

此外,本包还增加了评估逻辑方程的功能,根据Lee Eden在 https://www.phpclasses.org/discuss/package/2695/thread/4/ 发表的代码实现。

使用方法

<?php
	$math = new \Cognito\EvalMath\EvalMath;
	// basic evaluation:
	$result = $math->evaluate('2 + 2');
	// supports: order of operation; parentheses; negation; built-in functions
	$result = $math->evaluate('-8(5/2)^2 * (1 - sqrt(4)) - 8');
	// create your own variables
	$math->evaluate('a = e^(ln(pi))');
	// or functions
	$math->evaluate('f(x,y) = x^2 + y^2 - 2x*y + 1');
	// and then use them
	$result = $math->evaluate('3*f(42,a)');

	// Use the equations in logic strings
	$result = $math->evaluate('2 + 2 = 4'); // true
	$result = $math->evaluate('2 + 2 < 4'); // false
	$result = $math->evaluate('2 + 2 >= 4'); // true

	// Use dates as well, they are converted to seconds from epoch
	$result = $math->evaluate('date(y,m,d,h,m)');