本文整理汇总了PHP中Expression::getVariables方法的典型用法代码示例。如果您正苦于以下问题:PHP Expression::getVariables方法的具体用法?PHP Expression::getVariables怎么用?PHP Expression::getVariables使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Expression
的用法示例。
在下文中一共展示了Expression::getVariables方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testGetVariables
function testGetVariables()
{
$e = new Expression('2*x + y*sin(4*x)');
$v = $e->getVariables();
$this->assertEqual(sizeof($v), 2);
$this->assertEqual($v[0], 'x');
$this->assertEqual($v[1], 'y');
}
示例2: checkEquation
/**
* Check that an equation is correct with the variable values in this problem.
* All variables in the equation must be defined in this problem.
* @param $str :: An equation as a string.
* @return :: A string 'true' if the equation is correct and error message otherwise.
*/
function checkEquation($str)
{
$e = new Expression($str);
if ($e->fun != '=') {
return 'not equation';
}
$vars = $e->getVariables();
$undef = $this->findUndefinedVariables($vars);
if (sizeof($undef) > 0) {
return 'undefined: ' . implode(', ', $undef);
}
$lhs = $e->terms[0];
$rhs = $e->terms[1];
$fun = $rhs->op;
$lv = $lhs->eval_double($this->vars);
$rv = $rhs->eval_double($this->vars);
$res = 'oops';
if ($fun == '=') {
$res = $lv == $rv ? 'true' : 'false';
} else {
if ($fun == '>') {
$res = $lv > $rv ? 'true' : 'false';
} else {
if ($fun == '<') {
$res = $lv < $rv ? 'true' : 'false';
} else {
if ($fun == '>=') {
$res = $lv >= $rv ? 'true' : 'false';
} else {
if ($fun == '<=') {
$res = $lv <= $rv ? 'true' : 'false';
}
}
}
}
}
return $res;
}