PHP中的$ _POST如何运行

我的索引文件是

<body>
  <form method="POST" action="post.php" >
    <input name="name">
    <input type="submit" value="submit">
  </form>
  </body>

和post.php是

<?php 
$sec = 'qaswed';
if($sec == $_POST['name'])
{
    echo "true";
}
else
{
    echo "false";
}

?>

当我在post.php文件中简单地用TRUE代替$ _POST ['name']时,无论我在索引文件中提交什么,结果都是正确的,即

if($sec == TRUE) { echo "true"; } else { echo "false"; }//true

但是,当在索引文件中时,如果我在name参数中发送TRUE,那么为什么输出为假..即

if($sec == $_POST['name']) { echo "true"; } else { echo "false"; }// false when name=TRUE

当我从其他页面变量(请求)发送此消息时,它的计算结果为false。为什么会发生?

评论
  • lquia
    lquia 回复

    When you write if ($sec == TRUE), then it's true, because you are using automatic type conversion with the == operator, and php converts the $sec string to bool type, where since it's not (bool)false(not string 'false'!!!!) or (int)0, it becomes true, and the true === true = true.

    If you don't want the php to automatically convert the values in the if, then use === instead of ==, which will also check the type.

    在另一种情况下,您发送的是“ true”字符串,而您拥有的“ qaswed”字符串显然是不同的,并且由于它们都是字符串,因此没有像上一种情况那样进行类型转换。