显示$ x为undefined。我的switch语句出了什么问题?
http://localhost/add.php?price_error=1
switch(isset($_GET['price_error']) && $_GET['price_error'] == $x){
case 1:
echo '<span class="error_msg">Discount price cannot be greater than original price</span><br/>';
break;
case 2:
echo '<span class="error_msg">Discount cannot be less than 30% of original price</span><br/>';
break;
case 3:
echo '<span class="error_msg">Discount price and original price cannot be greater than $30000 HKD</span><br/>';
break;
default:
false;
break;
}
答案 0 :(得分:2)
这是因为你没有定义$x
变量(我假设你希望它保持price_error
id):
$x = isset($_GET['price_error']) ? (int)$_GET['price_error'] : 0;
switch($x) {
case 1:
echo '<span class="error_msg">Discount price cannot be greater than original price</span><br/>';
break;
case 2:
echo '<span class="error_msg">Discount cannot be less than 30% of original price</span><br/>';
break;
case 3:
echo '<span class="error_msg">Discount price and original price cannot be greater than $30000 HKD</span><br/>';
break;
default:
break;
}
答案 1 :(得分:1)
开关通常具有可变参数。
尝试:
if(isset($_GET['price_error']) && $_GET['price_error'] == $x){
switch($x){
//code
}
}