Home >Backend Development >PHP Tutorial >,字符串,运算符,整型相加减。怎么写式子?

,字符串,运算符,整型相加减。怎么写式子?

WBOY
WBOYOriginal
2016-06-23 13:15:381162browse



输出的$sum是一个式子
我希望它直接输出一个结果。
应该怎么办?


回复讨论(解决方案)

$a = '3';$b = '4';$c = '+';$sum = $a.$c.$b;echo $sum, PHP_EOL;//可以这样echo eval("return $sum;"), PHP_EOL;//也可以这样$f = create_function('', "return $sum;");echo $f();
3+4
7
7

$a = '3';$b = '4';$c = '+';$sum = $a.$c.$b;echo $sum, PHP_EOL;//可以这样echo eval("return $sum;"), PHP_EOL;//也可以这样$f = create_function('', "return $sum;");echo $f();
3+4
7
7



感谢帮助
echo eval("return $sum;");
这段代码是有用的。
后面的那一段定义的变量f测试的时候发现出错。

拜托你抄仔细了

$f = create_function('', "return $sum;");echo $f();

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn