<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语句中对问题1使用'',但在echo语句中没有引号。
简而言之:为什么$ _POST ['question1']在if语句中有''
以及为什么$ _POST [question1]在echo语句中没有。它们是相同的变量。
谢谢你。
最佳答案
始终使用引号(用于字符串键),除非在双引号引起来的字符串内。请参见手册的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;
关于PHP的方括号: when to put quotes and when not?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12333087/