“true == isset($variable)”和“true == isset($variable)”之间的区别和“isset($variable) == true”在 PHP 中

发布于 2024-12-29 10:20:57 字数 167 浏览 0 评论 0原文

我只是在思考 PHP 中条件相等的概念,即

之间有什么区别

true == isset($variable) 

isset($variable) == true

I was just wandering the about the concept of equating the condition in PHP that is,

what could be the difference between

true == isset($variable) 

and

isset($variable) == true

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

破晓 2025-01-05 10:20:57

对于这个具体情况,没有区别。

第一种语法用于防止意外赋值而不是比较。

if ( true = $x ) // would yiled error
if ( $x = true ) // would work

但同样,就你的情况而言,没有区别。

详细说明:

假设您想要将变量 $xtrue 进行比较并执行某些操作。你可能会不小心写成“

if ( $x = true )

of”

if ( $x == true )

,并且条件总是会通过。

但如果你养成了编写的习惯,

if ( true == $x )

这些错误就不会发生,因为会生成语法错误,而你会提前知道。

For this specific case, no difference.

The first syntax is used to prevent accidental assignment instead of comparison.

if ( true = $x ) // would yiled error
if ( $x = true ) // would work

But again, in your case, no difference.

Elaboration:

Say you want to compare a variable $x to true and do something. You could accidentally write

if ( $x = true )

instead of

if ( $x == true )

and the condition would always pass.

But if you get into the habit of writing

if ( true == $x )

these mistakes wouldn't happen, since a syntax error would be generated and you would know in advance.

淡墨 2025-01-05 10:20:57

没有什么区别。但isset()本身返回一个布尔值。

所以永远不要使用

if (true == isset($variable))

只是:

if (isset($variable))

There is no difference. But isset() itself returns a boolean value.

So never use

if (true == isset($variable))

Just:

if (isset($variable))
倾城花音 2025-01-05 10:20:57

请记住,当 php 解析时,true 已实际定义,并且它等于 1。此外,false 也是如此,它等于 0。php 会自动检查这些值,以便与 IF 语句中的这些值进行比较。您将安全使用!运算符,因为它与 if ($something == false) 相同,祝你好运!

Remember that when php parses that true is actually defined and its equal to 1. Furthermore so is false and it is equal to 0. php automatically checks these for comparison with these values in an IF statement. You'll be safe using the ! operator, because its the same as if ($something == false) good luck!

浅唱ヾ落雨殇 2025-01-05 10:20:57

如果是Java,那就有区别了。

IE

  String test = null;
  if("".equals(test)){
    System.out.println("I m fine..");
  }
  if(test.equals("")){
    System.out.println("I m not fine..");
  }

Would it be Java, there was difference.

i.e.

  String test = null;
  if("".equals(test)){
    System.out.println("I m fine..");
  }
  if(test.equals("")){
    System.out.println("I m not fine..");
  }
梦境 2025-01-05 10:20:57

不存在“真正的”差异(在本例中)
之间
true == isset($variable)
AND

 isset($variable) == true

There is no "real" diffrences(in this case)
Between
true == isset($variable)
AND

 isset($variable) == true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文