<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语句中没有。它们是同一个变量。 谢谢。
答案 0 :(得分:3)
始终使用引号(对于字符串键),除非在双引号字符串中。请参阅手册的the string parsing section。
$juices = array("apple", "orange", "koolaid1" => "purple");
echo "He drank some $juices[0] juice.".PHP_EOL;
echo "He drank some $juices[1] juice.".PHP_EOL;
echo "He drank some juice made of $juice[0]s.".PHP_EOL; // Won't work
echo "He drank some $juices[koolaid1] juice.".PHP_EOL;
答案 1 :(得分:0)
数组键的字符是文字,因此文本应该有单引号。整数键不应该有引号。
以下是更精细的细节:
$var[key]
将被解释为$var['key']
。echo "Your ID is {$user['id']}."
。$var["someKey$num"]
。即使在这种情况下,最好使用$var['someKey'.$num]
或$var["someKey{$num}"]
。