-5

我有一个问题要问你,希望你能帮助我。我想写计算器但我没有机会改变操作,我只能使用加法操作。我不知道发生了什么。谁能帮我?

看看它,发生了什么?

<?php
$x = isset($_POST['field']);
if($x == 1){ 
    echo $x + 5;
}
elseif($x == 2){
    echo $x - 5;
}
?>
4

2 回答 2

2

You code is wrong as this line will evaluate only as 0 (false) or 1 (true) if condition is met:

$x = isset($_POST['field']);  

You need to do it that way:

$x = isset($_POST['field']) ? $_POST['field'] : 0;

(where 0 is default value, $x is assigned to in case there's no $_POST['field]` set.

于 2013-07-06T13:46:06.560 回答
0
$x = isset($_POST['field']) ? (int)$_POST['field'] : 0;

You want the value, not the boolean if it's set or not.

于 2013-07-06T13:46:27.653 回答