php怎么可能既不执行'then'也不执行'if'的'else'子句呢?

How can php not execute neither 'then' nor 'else' clauses of 'if'?

this tutorial之后(我适应了PostgreSQL),我在register.inc.php中遇到了问题:

if (empty($error_msg)) {
    // Create a random salt
    $random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
    // Create salted password 
    $pwd = hash('sha512', $pwd.$random_salt);
    // Insert the new user into the database 
    $q = "insert into usr (username,email,pwd,salt) values (,,,);";
    $res = pg_query_params($conn,$q,[$username,$email,$pwd,$random_salt]);
    if ($res === false) {
        header("Location: ../html/coisas/error.php?err=Registration failure: INSERT");
    } else {
        header('Location: ./register_success.php?msg=1'.$random_salt);
    }
    header('Location: ./register_success.php?msg=2'.$random_salt);
}

发送的header是第三个(?msg=2...)。如果我将其注释掉,则不会发送 header。怎么可能不进入 then 子句,也不进入 else 子句呢?数据未存储在数据库中,但我收到 "sucess" 响应。我怎样才能确定 $res 的值?

A header() 仅当它在任何其他输出发送到浏览器之前发送时才有效。我猜你已经向浏览器发送了一些东西,也许是一条调试消息或其他东西。

如果发生这种情况,您应该会在 php error log

中看到一条自我解释的错误消息

此外,您应该始终在任何 header() 语句之后执行 exit;,因为 header() 实际上不会停止脚本其余部分的执行。

if ($res === false) {
    header("Location: ../html/coisas/error.php?err=Registration failure: INSERT");
    exit;
} else {
    header('Location: ./register_success.php?msg=1'.$random_salt);
    exit;
}
header('Location: ./register_success.php?msg=2'.$random_salt);
exit;