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

php动态加减乘除

WBOY
Release: 2016-06-06 20:25:47
Original
2272 people have browsed it

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

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

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

回复内容:

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

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

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

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

关于eval,参考文档

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

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template