如果我的开关程序中的语句不起作用。我究竟做错了什么?

时间:2016-01-27 13:53:12

标签: php if-statement switch-statement

该程序应该显示用户的下拉选择,并使用php中的switch语句输出该特定选项的响应。我不能让我的if语句工作。有谁可以帮助我吗?谢谢大家。

<!doctype html>
<html>
<head>
   <title>Program 2</title>
</head>
<body>



<form action="<?php $PHP_SELF; ?>" method="post">

<select name="pick">
<option value="regular">I am a regular customer</option>
<option value="friend">From a friend</option>
<option value="television">On television</option>
<option value="online">In an online search</option>

</select>
<input type="submit" value="Submit Form"><br>
</form>


<?php  

    $choice = $_POST['pick'];

    if($choice($_POST['pick']) echo "Excellent. We love our regular     customers!";) {
} else {
switch($choice) {
    case 'regular':
         echo "Excellent. We love our regular customers!";
         break;
    case 'friend':
         echo "Please thank your friend for us.";
         break;
    case 'television':
         echo "We are glad to hear our TV ads are working.";
         break;
     case 'online':
         echo "We work hard to be found on Google.";
         break;
    }
   }
?>

2 个答案:

答案 0 :(得分:2)

您的代码中有很多随机复制/粘贴错误...而是使用:

<!doctype html>
<html>
    <head>
       <title>Program 2</title>
    </head>
    <body>
        <form action="" method="post">
            <select name="pick">
                <option value="regular">I am a regular customer</option>
                <option value="friend">From a friend</option>
                <option value="television">On television</option>
                <option value="online">In an online search</option>
            </select>
            <input type="submit" value="Submit Form"><br>
        </form>


        <?php
            if(isset($_POST['pick'])) {
                switch($_POST['pick']) {
                    case 'regular':
                         echo "Excellent. We love our regular customers!";
                         break;
                    case 'friend':
                         echo "Please thank your friend for us.";
                         break;
                    case 'television':
                         echo "We are glad to hear our TV ads are working.";
                         break;
                     case 'online':
                         echo "We work hard to be found on Google.";
                         break;
                }
           }
        ?>
    </body>
</htm

答案 1 :(得分:0)

if / else的正确语法如下:

if ($conditions)
{
  echo "conditions is true";
}
else
{
  echo "conditions is false";
}

您的代码相当于:

if ($some_conditions)
  echo "condition is true";
// without braces, only the 1st instruction is concerned by the if 

{
echo "conditions is true";
}
else // syntax error here, because the "else" 
{
  echo "conditions is false";
}

当您编辑代码时,可以在脚本顶部添加此代码以获取有关错误的更多信息:

<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
?>