这个if/else块中的回显在脚本结束时不起作用


This echo inside an if/else block is not working at the end of a script

我在这里看了类似的问题,也做了一个基本的谷歌搜索,但我能找到的最好的是关于为什么W3Schools是一个可怕的学习场所,这对我的问题没有多大帮助。我不一定想要一个解决方案,我更喜欢一个解释,为什么这是不工作,因为我有一个类似的问题在过去,但工作,因为这是我简单地调试和测试其他部分的代码。

首先,我在一个不打印的if语句中有一个讨厌的回显,有趣的是if和else块似乎都没有被执行。

我的index.php文件:

<!DOCTYPE html>
<html>
    <head>
        <title>A Title</title>
    </head>
    <body>
        <?php
            session_start();
            include "scripts/Employee.php";
            error_reporting(E_ALL);
            $user = new Employee('bbuck', 'password');
            echo "<p>I have some stuff that works here.</p>";
            if (!($user->error() === false)) {
                echo "<p>Authenticated!</p>";
            }
            else {
                echo $user->error();
            }
        ?>
    </body>
</html>

Employee构造函数:

public function __construct($username, $password) {
    $db = Database::getInstance();
    $result = $db->authenticate($username, $password);
    if ($result !== false) {
        $this->id = $result['id'];
        $this->fname = $result['fname'];
        $this->lname = $result['lname'];
        $this->lastAction = $result['last_action'];
        $this->error = false;
    }
    else
        $this->error = "Failed to authenticate the user, wrong username/password combination.";
}

我不认为这真的很重要,只是作为参考,Employee::error()函数就是简单的return $this->error;

最后是Database::authenticate()函数

public function authenticate($username, $password) {
    $query = "SELECT * FROM `employee` WHERE `username` = '" 
        . $this->conn->escape_string($username) 
        . "' AND `password` = PASSWORD('"
        . $this->conn->escape_string($password) . "')";
    $result = $this->conn->query($query);
    if (!$result)
        return false;
    $rowCount = $result->num_rows;
    if ($rowCount == 1) {
        $row = $result->fetch_assoc();
        $update = "UPDATE `employee` SET `session_id` = UUID(), `session_expires` = (NOW() + INTERVAL 10 MINUTE) WHERE `id` = {$row['id']}";
        $select = "SELECT `session_id`, `session_expires` FROM `employee` WHERE `id` = {$row['id']}";
        $updateResult = $this->conn->query($update);
        if (!$updateResult)
            die("Failed to update the employee!");
        $updateResult = $this->conn->query($select);
        if (!$updateResult)
            die("Failed to pull Session data for employee!");
        $updateRow = $updateResult->fetch_assoc();
        $_SESSION[SESSION_ID] = $updateRow['session_id'];
        $_SESSION[SESSION_EXPIRES] = new DateTime($updateRow['session_expires']);
        return $row;
    }
    return false;
}

现在,index.phpif/else块中的echo语句没有打印任何东西,我在$user$user->error() === false上使用了var_dump,并且都给了我期望从它们那里收到的结果。我还在if语句之前放置了一个echo,它工作了,以及一个在语句之后,它也工作了。我不明白为什么他们被忽视了。

这是为了调试,我试图测试代码,但正如我所说的,我很好奇为什么这被跳过,因为我以前遇到过这个

我不确定这是否是你的问题的全部,但你的条件不正确。

if (!($user->error() === false)) {
    echo "<p>Authenticated!</p>";
}
else {
    echo $user->error();
}

else语句中的echo $user->error();只会在$user->error() === false。在本例中,您将执行echo false;,它不会给您任何回显输出。


要解决此问题,请使用以下命令(假设$user->error();返回false,如果不存在错误):

if ($user->error() === false) {
    echo "<p>Authenticated!</p>";
}
else {
    echo $user->error();
}

@adlawson是对的。您可以在php.ini中查看display_errors是否设置为On。如果不是,将其设置为ON,然后执行脚本,看看是否会出现错误。有时,如果$class->method()返回一个错误,您可能看不到它,因为display_errors=Off