PHP MySQL 比较始终为 true,无论操作数和运算符如何
作为记录,我阅读了有关 PHP 中布尔比较的文档,但我不明白我的代码与此上下文有何关联。我以为连接 MySQL 时遇到问题,但事实证明一切正常,我可以毫无问题地进行查询。所以,MySQL 与这篇文章并没有那么相关。
我用它来报告尝试在构造时连接的对象中的错误。请注意,我尝试了 ==、===、!= 和 !== 只是为了得到同样的问题。我什至尝试将参数转换为 bool 和 boolean。
以下代码块中的注释是相关的。
private function assert($test){ // Tried renaming in case PHP was funny about it.
if ($test === FALSE){ // Tried ==, ===, !== and != and casting $test.
if ($this->use_exception){
throw new mysql_exception();
}else{
die("MySQL ".mysql_errno()." - ".mysql_error());
}
}
}
连接是典型的。
$this->con = mysql_connect($host, $un, $pw, false, $ssl ? MYSQL_CLIENT_SSL : 0);
// I get 'yay'
echo mysql_ping($this->con) ? "yay" : "nay";
// This disagrees. Tried no cast, and a cast to bool.
$this->assert((boolean)($this->con != FALSE));
mysql_ping() 表示一切正常,但assert() 无论如何都会停止按下。
我尝试了每一个运算符和演员组合,甚至出于对名称冲突的偏执而重命名了该函数。为什么assert()只看到true?
编辑:
为了使我的问题更清楚,请考虑使用 Eugen 建议使用 is_resource 的替代实现。问题是我不知道为什么会发生以下情况。
private function assert($test){
$test = $test ? true : false;
if ($test === FALSE){
echo "$test === false<br />";
}
if ($test == FALSE){
echo "$test == false<br />";
}
if ($test !== FALSE){
echo "$test !== false<br />";
}
if ($test == FALSE){
echo "$test != false<br />";
}
}
输出乱序,并且在一次比较后值发生变化。因为我可以进行查询,所以 $test 一定是 true。我根本不应该得到输出,但我对每个操作员都得到输出。我也尝试过将 FALSE 替换为 0。
错误的 PHP 实例?
1 !== false
=== false
!== false
!= false
For the record, I read documentation on boolean comparisons in PHP, but I do not understand how my code relates in this context. I thought I had a problem connecting to MySQL, but it turns out everything is fine and I can make queries without issue. So, MySQL is not really that relevant to this post.
I use this to report errors in an object that tried to connect on construction. Note that I tried ==, ===, != and !== only to get the same problem. I even tried casting the argument to bool and boolean.
Comments in below code blocks are relevant.
private function assert($test){ // Tried renaming in case PHP was funny about it.
if ($test === FALSE){ // Tried ==, ===, !== and != and casting $test.
if ($this->use_exception){
throw new mysql_exception();
}else{
die("MySQL ".mysql_errno()." - ".mysql_error());
}
}
}
Connecting is typical.
$this->con = mysql_connect($host, $un, $pw, false, $ssl ? MYSQL_CLIENT_SSL : 0);
// I get 'yay'
echo mysql_ping($this->con) ? "yay" : "nay";
// This disagrees. Tried no cast, and a cast to bool.
$this->assert((boolean)($this->con != FALSE));
mysql_ping() says everything is ok, but assert() stops the presses no matter what.
I tried every operator and cast combination, even renaming the function out of paranoia over a name clash. Why does assert() only see true?
EDIT:
To make my problem clearer, consider the alternative implementation with Eugen's suggested use of is_resource. The problem is that I just have no idea why the below happens.
private function assert($test){
$test = $test ? true : false;
if ($test === FALSE){
echo "$test === false<br />";
}
if ($test == FALSE){
echo "$test == false<br />";
}
if ($test !== FALSE){
echo "$test !== false<br />";
}
if ($test == FALSE){
echo "$test != false<br />";
}
}
Output is out of order and the value changed after one comparison. Since I can make queries, $test must be true. I should not get output at all, but I do for every operator. I also tried it with FALSE replaced by 0.
Bad PHP instance?
1 !== false
=== false
!== false
!= false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用第二个示例并合并该功能吗? 你就已经得到了一个值(是/否)
只要大声思考,
can't you use your second example and combine the function? you already get a value (yay/nay)
just thinking out loud