为什么在 PHP 中 true 大于 3

2023-07-15php开发问题
2

本文介绍了为什么在 PHP 中 true 大于 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道为什么 PHP 中的以下语句返回 true?

I am wondering why following statement in PHP is returning true?

true>=4

例如这样的行会回显 1

echo true>=4;

谁能解释一下这背后的逻辑?

Can anyone explain me the logic behind this?

推荐答案

除了 Davids 的回答之外,我还想添加一些东西来增加深度.

In addition to Davids answer, I thought to add something to give a little more depth.

PHP 与其他编程语言不同,如果您不小心操作符/语法,您可能会陷入像您遇到的那种棘手的坑洞.

PHP unlike other programming languages, if your not careful with your operators/syntax you can fall into tricky pot holes like the one you experience.

正如大卫所说,

4 也是真(因为它非零),真等于真,所以它也大于或等于 true.

4 is also true (because it's non-zero), and true is equal to true, so it's also greater than or equal to true.

考虑到这一点真大于假.

真 = 1

假=0

举个例子:

$test = 1;
if ($test == true){
echo "This is true"; 
}else{
echo "This is false";
}

上面会输出

这是真的

但是如果你接受这个:

$test = 1;
if ($test === true){
echo "This is true"; 
}else{
echo "This is false";
}

上面会输出:

这是假的

添加的等号查找完全匹配,从而查找 integer 1 而不是 PHP 读取 1 为真.

The added equals sign, looks for an exact match, thus looking for the integer 1 instead of PHP reading 1 as true.

我知道这有点离题,但只是想解释一下 PHP 包含的一些坑.

希望对您有所帮助

回答你的问题:

回声真>=4;

您看到 1 作为输出的原因是因为 true/false 被解释为数字(见上文)

Reason you are seeing 1 as output, is because true/false is interpreted as numbers (see above)

无论你是在做 echo true>=4 还是只是 echo true; php 都将 true 设为 1,将 false 设为 0

Regardless if your doing echo true>=4 or just echo true; php puts true as 1 and false as 0

这篇关于为什么在 PHP 中 true 大于 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17