为什么“;退出”,导致页面根本不显示";完成任务


why does "exit" , causes page not to display at all, while echoing " " gets the job done?

我正在研究php,我想知道为什么第6行的exit;会导致页面变白,而在第7行使用echo " ";会使其按预期工作。这是代码:

<?php 
 if ($_POST['username']=="user" && $_POST['password']=="password") {
    header("Location: game.html");
}
else {
  //(if i write exit on this line, i get all white page)  exit;
  // (if i echo empty string the site works ok)     echo "";
} ?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h3 style="text-align: center">რეგისტრაცია</h3>
    <div style="margin: 0 300px 0 300px">
    <form action="index.php" method="post">
    მომხმარებელი: <input type="text" name="username" value=""><br />
    პაროლი: <input type="password" name="password" value="" style="margin-left: 47px"><br/>
            <input type="submit" name="submit" value="შესვლა" style="margin-left: 170px">
     </form>
     </div>
</body>

这是我的真实网站-用于学习目的http://nikolozasatiani.com/,登录后即可享受游戏:D用户名:用户密码:密码

语言构造exit停止执行,即下面的所有内容(在您的情况下是HTML)永远不会被发送到输出缓冲区,因为PHP不会解析超过该点的任何内容。

但是,在执行echo " ";时,您只需在页面上打印一个空格。在您的示例中,您可以省略else部分,因为它什么都不做:

if ($_POST['username']=="user" && $_POST['password']=="password") {
    header("Location: game.html");
}

实际上,由于if语句中只执行一行,如果您更喜欢这种语法,也可以省略花括号。就我个人而言,我认为它看起来更干净:

if ($_POST['username']=="user" && $_POST['password']=="password")
    header("Location: game.html");