Home >Backend Development >PHP Tutorial >php动态加减乘除

php动态加减乘除

WBOY
WBOYOriginal
2016-06-06 20:25:472267browse

<code>$a = 1;
$b = 1;
$c = '-';
echo $a.$c.$b;//1+1</code>

要怎么实现$a.$c.$b输出0?

能不能实现传入加减乘除动态计算结果吗?

回复内容:

<code>$a = 1;
$b = 1;
$c = '-';
echo $a.$c.$b;//1+1</code>

要怎么实现$a.$c.$b输出0?

能不能实现传入加减乘除动态计算结果吗?

<code>echo eval("return {$a}{$c}{$b};");</code>

关于eval,参考文档

<code class="php">echo $a.'-'.$b.'='.($a-$b);</code>

switch($c){
case '+':
echo $a+$b;break;
case '-':
echo $a-$b;break;
case '*':
echo $a*$b;break;
case '/':
echo $a/$b;break;
default:
echo "输入错误";
}

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