这很简单,但我无法理解逻辑。
有一个简单的在线表格。希望能够在提交后调整变量。
如果有人能够阐明这一点,那将是非常感激的!
$chocolatebar = $HTTP_POST_VARS['chocolatebar'];
if $chocolatebar = "a" then we want to change the value to "snickers";
if $chocolatebar = "b" then we want to change the value to "mars";
if $chocolatebar = "c" then we want to change the value to "cherry ripe";
答案 0 :(得分:3)
$chocolatebar = isset($_POST['chocolatebar']) ? $_POST['chocolatebar'] : '';
if ($chocolatebar == "a") $chocolatebar = "snickers";
elseif ($chocolatebar == "b") $chocolatebar = "mars";
elseif ($chocolatebar == "c") $chocolatebar = "cherry ripe";
答案 1 :(得分:1)
$chocolatebar = $POST['chocolatebar'];
if($chocolatebar =="a")
{
$chocolatebar="snickers";
}
else if($chocolatebar =="b")
{
$chocolatebar="mars";
}
else
{
$chocolatebar="cherry ripe";
}
答案 2 :(得分:1)
首先不要使用$HTTP_POST_VARS
,不推荐使用此功能$HTTP_POST_VARS
另外,我猜你应该使用switch而不是if-else。
<?php
$chocolatebar = ($_POST['chocolatebar'] != '') ? $_POST['chocolatebar'] : '';
switch($chocolatebar){
case 'a':
$chocolatebar = 'snickers';
break;
case 'b':
$chocolatebar = 'mars';
break;
case 'c':
$chocolatebar = 'cherry ripe';
break;
default:
$chocolatebar = '';
break;
}
echo $chocolatebar;
答案 3 :(得分:0)
在这种情况下,当您有多个选项时,最好使用switch
,然后使用多个if-else
。
if(isset($_POST['chocolatebar'])){
$chocolatebar = $_POST['chocolatebar'];
switch ($chocolatebar) {
case "a":
$chocolatebar = "snickers";
break;
case "b":
$chocolatebar = "mars";
break;
case "c":
$chocolatebar = "cherry ripe";
break;
}
}
答案 4 :(得分:0)
$chocolatebar = $_POST['chocolatebar'];
switch($chocolaterbar){
case "a":
$chocolatebar = "snickers";
break;
case "b":
$chocolatebar = "mars";
break;
case "c":
$chocolatebar = "cherry ripe";
break;
default:
$chocolatebar = "unknown";
break;
}
我的目的是使用开关。或者另一个智能选项是使用数组..
$all_chocolates = array("a"=>"snickers","b"=>"mars","c"=>"cherry ripe");
$chocolatebar = $_POST['chocolatebar'];
$chocolatebar = isset($all_chocolates[$chocolatebar]) ? $all_chocolates[$chocolatebar] : "unknown";
答案 5 :(得分:0)
如果您想更改更多值,也可以使用开关案例。
答案 6 :(得分:0)
为方便起见,您需要在键值之间进行映射。
$valueMap = array(
'a' => 'snickers',
'b' => 'mars',
'c' => 'cherry ripe'
);
// The '@' sign prevents error if characters other than
// 'a', 'b', 'c' is provided, $value will be NULL in
// those cases.
$value = @$valueMap[$_POST['chocolatebar']];