0

これはとても簡単ですが、私は論理に頭を入れることができません。

簡単なオンラインフォームを用意してください。送信後に変数を調整できるようにしたい。

誰かがこれに光を当てることができれば、本当に感謝しています!

$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"; 
4

7 に答える 7

3
$chocolatebar = isset($_POST['chocolatebar']) ? $_POST['chocolatebar'] : '';
if ($chocolatebar == "a")  $chocolatebar = "snickers";
elseif ($chocolatebar == "b")  $chocolatebar = "mars";
elseif ($chocolatebar == "c")  $chocolatebar = "cherry ripe"; 
于 2012-11-02T05:55:09.957 に答える
1

まず第一に$HTTP_POST_VARS、この関数は非推奨です$HTTP_POST_VARS

また、if-elseの代わりにswitchを使用する必要があると思います。

<?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;
于 2012-11-02T06:06:57.383 に答える
1
$chocolatebar = $POST['chocolatebar'];

if($chocolatebar =="a")
{ 
     $chocolatebar="snickers";
}
else if($chocolatebar =="b")
{ 
    $chocolatebar="mars";
}
else
{ 
    $chocolatebar="cherry ripe";
}
于 2012-11-02T05:56:45.927 に答える
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']];
于 2012-11-02T06:03:43.143 に答える
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;
    }
}
于 2012-11-02T06:01:26.113 に答える
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;
}

My purpose is to use switch. Or one other smart option is to use array..

$all_chocolates = array("a"=>"snickers","b"=>"mars","c"=>"cherry ripe");
$chocolatebar = $_POST['chocolatebar'];
$chocolatebar = isset($all_chocolates[$chocolatebar]) ? $all_chocolates[$chocolatebar] : "unknown";
于 2012-11-02T06:02:37.127 に答える
0

You can also use switch case if you want to change more value.

于 2012-11-02T06:03:29.037 に答える