Php 我的switch程序中的If语句不';不行。我做错了什么?
这个程序应该为用户显示一个下拉选择,并使用php中的switch语句输出来自该特定选择的响应。不过,我的if语句无法正常工作。有人能帮我吗?谢谢大家Php 我的switch程序中的If语句不';不行。我做错了什么?,php,if-statement,switch-statement,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"
<!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
您的代码中有大量随机复制/粘贴错误。。。而是使用:
<!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
方案2
我是常客
来自朋友
在电视上
在网上搜索
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");
?>
或直接使用.,.jquery如果你只是简单地根据选择值回显字符串,我建议你检查实际的php教程以避免常见错误(这里,我看到$php\u SELF
,括号与$choice($\u POST['pick'])一起使用无效。
。如果这是某位老师给出的练习,并且没有说明“使此代码有效”,请告诉他更新其课程。