【发布时间】:2012-09-02 05:19:12
【问题描述】:
<html>
<head><title></title></head>
<body>
<?php
if (isset ($_POST['posted'])) {
if ($_POST['question1'] == "Lisbon") {
echo "You are correct, $_POST[question1] is the right answer<hr>";
}
if ($_POST['question1'] != "Lisbon") {
echo "You are incorrect, $_POST[question1] is not. the right answer<hr>";
}
}
?>
<form method="POST" action="quiz.php">
<input type="hidden" name="posted" value="true">
What is the capital of Portugal?
<br>
<br>
<input name=''question1" type=''radio" value=''Porto''>
Porto
<br>
<input name=''question1" type="radio" value=''Lisbon''>
Lisbon
<br>
<input name="question1" type="radio" value=''Madrid''>
Madrid
<br>
<br>
<input type=''submit''>
</form>
</body>
</html>
这是整个部分,它来自 PDF。但问题是,他们没有说明为什么他们在 if 语句中使用 question1 为 ' ' 而在 echo 语句中没有引号。
简而言之:为什么 $_POST['question1'] 在 if 语句中有 ' ' 以及为什么 $_POST[question1] 在 echo 语句中没有。它们是同一个变量。 谢谢。
【问题讨论】:
-
对于数组值,键可以是字符串(带引号)或数字。对于在双引号字符串中写入数组值,不需要引号。请参阅php.net/string(向下滚动到“变量解析”)。
-
不相关,但看起来你的一些属性引号搞砸了,使用
''(两个单引号)而不是"(一个双引号)。不确定这只是您的帖子中的错误还是您的实际代码中的错误,但您可能想检查一下。
标签: php variables post quotes brackets