PHP中的错误比较


Wrong comparison in PHP

我制作了一个PHP函数来将一些数据存储在数据库中。然而,当我测试它时,一个比较出现了错误。它应该发现一个帖子包含一个带false的布尔值。当在屏幕上打印时,它看起来很好,然而,这种比较仍然是错误的。这是我认为重要的代码。

PHP函数:

$FaultMss = "";
$chpw = "";
if($login){
if(isset($_POST['Save_Changes'])){
    $nun = $db->real_escape_string($_POST['nun']);
    $nem = $db->real_escape_string($_POST['nem']);
    $ncn = $db->real_escape_string($_POST['ncn']);
    $nct = $db->real_escape_string($_POST['nct']);
    if(isset($_POST['chpw'])){
        $chpw = "checked";
        $opw = $_POST['opw'];
        $npw = $_POST['npw'];
        $cnpw = $_POST['cnpw'];
    }
    print_r($_POST['remove']);    //This outputs the correct value
    if($_POST['remove'] == true){   //Here it goes wrong. I should get false when pressing the html link "save changes". But I always get true.
        print_r($_POST['remove']);   //This outputs the correct value
        $FaultMss = "A removement";
    }else{
        $FaultMss = "Not a removement";
    }
}else{
    $uns = $db->real_escape_string($un);
    $Result = mysqli_query($db, "SELECT * FROM `accounts` WHERE `Username`='" . $uns . "'");
    $row = $Result->fetch_object();
    $nun = $un;
    $nem = $row->Email;
    $ncn = $row->Country;
    $nct = $row->City;
}
}

print_r函数显示了良好的值。所有数据库查询都是正确的$联合国在此之前就已经成立了。在这之前也建立了联系。

HTML表单:

<div id="Personal_info_wrapper" style="overflow:auto; position:absolute; top:40; left:40; width:460; height:430;">
<?php 
if($login){
    echo '<form id="personalInfo" action="" method="POST">
    <label for="un">Username:</label><br/>
        <input type="text" id="un" name="nun" value="' . $nun . '"/><br/>
    <br/>
        <input type="checkbox" id="chpw" name="chpw" value=true ' .$chpw. '/><label for="chpw">I want to change my password</label><br/>
    &nbsp;<label for="opw">Old password</label><br/>
        &nbsp;<input type="password" id="opw" name="opw"/><br/>
    &nbsp;<label for="npw">New password</label><br/>
        &nbsp;<input type="password" id="npw" name="npw"/><br/>
    &nbsp;<label for="cnpw">Verify new password</label><br/>
        &nbsp;<input type="password" id="cnpw" name="cnpw"/><br/>
    <label for="em">E-mail:</label><br/>
        <input type="email" id="em" name="nem" value="' . $nem . '" style="width:200px;"/><br/>
    <label for="cn">Country:</label><br/>
        <input type="text" id="cn" name="ncn" value="' . $ncn . '"/><br/>
    <label for="ct">City:</label><br/>
        <input type="text" id="ct" name="nct" value="' . $nct . '"/><br/>
        <input type="hidden" name="Save_Changes" value=true/>
        <input type="hidden" name="Tab" value=4/>
        <input type="hidden" id="remove" name="remove" value=false />
    <br/>
    <a href=''Javascript:document.forms["personalInfo"].submit();''>Save changes</a>  &nbsp;  <a href="#" onclick="ConfirmRemoval()">Delete this account</a><br/>
    ' . $FaultMss . '</center>
    </form>';
}

该表单由PHP制作,以确保人们登录。变量都已设置。有一点JavaScript,如果按下Delete this account按钮,它会将remove值更改为true,但这并不重要。我试过用if($_POST['remove'])代替if($_POST['reve']==true),但没有效果。我检查了语法,但似乎没有什么问题。这里有什么问题?为什么我总是说实话?

它返回true,因为有一个值与$_POST['remove']关联-除非没有值,或者该值设置为false,否则它将始终返回true。

尝试===而不是===:)

编辑:示例:

<?php
   $lol = 'man this is a string';
   echo ($lol) ? 'true (==)' : 'false (==)'; echo "'n";
   echo ($lol===true) ? 'true (===)' : 'false (===)';
   /*
     Output:
     true (==)
     false (===)
   */
   $lol = true;
   echo ($lol) ? 'true (==)' : 'false (==)'; echo "'n";
   echo ($lol===true) ? 'true (===)' : 'false (===)';
   /*
     Output:
     true (==)
     true (===)
   */