1
我在PHP新的,我有3個參數的函數計算器在PHP不帶開關
function cal($x,$y,$op){
$z=$x.$op.$y;
return $z;
}
echo cal(3,4,'/');
而且我也試試這個:
$z=$x$op$y;
和我得到3/4
的答案,但我想0.75
我在PHP新的,我有3個參數的函數計算器在PHP不帶開關
function cal($x,$y,$op){
$z=$x.$op.$y;
return $z;
}
echo cal(3,4,'/');
而且我也試試這個:
$z=$x$op$y;
和我得到3/4
的答案,但我想0.75
試試這個:
function cal($x,$y,$op){
$z=$x.$op.$y;
return calculate_string($z);
}
echo cal(3,4,'/');
function calculate_string($mathString) {
$mathString = trim($mathString); // trim white spaces
$mathString = ereg_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString); // remove any non-numbers chars; exception for math operators
$compute = create_function("", "return (" . $mathString . ");");
return 0 + $compute();
}
這是因爲現在你只是建立一個字符串.. – Naruto