应该使用 if ($a != NULL) 还是 if ($a !== NULL) 来控制程序流程?

发布于 2024-12-27 08:33:25 字数 604 浏览 1 评论 0原文

这可能是一个令人痛苦的基本问题,但我想知道使用 PHP 的 if equal !==if equal 的性能问题!= 控制流程。

考虑以下简单的 PHP 函数:

<?php
 function test_json($json = NULL) {
  if ($json != NULL) {
   echo 'You passed some JSON.';
  } else {
   echo 'You failed to pass any JSON.';
  }
 }
?>

从性能的角度来看,是否最好使用 如果相同 (!==) 来防止 PHP 迭代变量类型,试图找到一个有效的比较?

假设 !== 首先比较变量类型,如果失败,它会立即返回FALSE? 从 PHP3 开始我几乎就本能地使用了 != 。现在我正在从事一些计算密集型项目,微小的性能考虑变得更加令人担忧。

当然,欢迎对流量控制优化提出其他意见!

This is perhaps a painfully basic question to answer, but I'm wondering about performance issues regarding using PHP's if identical !== versus if equal != to control flow.

Consider the following trivial PHP function:

<?php
 function test_json($json = NULL) {
  if ($json != NULL) {
   echo 'You passed some JSON.';
  } else {
   echo 'You failed to pass any JSON.';
  }
 }
?>

From a performance standpoint, is it preferable to employ if identical (!==) to prevent PHP iterating through variable types, attempting to find a valid comparison?

I assume that !== first compares the variable types, and if that fails, it immediately returns FALSE?
I've used != since PHP3 almost as a reflex. Now that I'm working on some much more computationally-intensive projects, minute performance considerations become more of a concern.

Other comments on flow control optimization are, of course, welcome!

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

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

发布评论

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

评论(2

赠我空喜 2025-01-03 08:33:25

我还没有对松散与严格比较运算符进行任何性能测试,但对于您想要做的事情,我建议您

if (!is_null($json)) {
    do_stuff()
}

http://www.php.net/manual/en/function.is-null.php

编辑:我链接的 php 页面注释中的注释上面的一些结果表明 === 运算符比 == 运算符稍快,两者都比 is_null() 快>。然而,另一条注释指出“ ===NULL 和 is_null 之间的执行时间差异小于 250 纳秒。去优化重要的东西。”我不得不同意这一点。综上所述,我建议您选择您认为最具可读性的内容。

I haven't done any performance tests on loose vs strict comparison operators, but for what you are trying to do, I would instead recommend something like

if (!is_null($json)) {
    do_stuff()
}

More information on is_null() at http://www.php.net/manual/en/function.is-null.php

EDIT: a note in the comments of the php page I linked to above has some results showing that the === operator is slightly faster than the == operator, both of which are faster than is_null(). However, another note points out that "The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters." I'd have to agree there. So all that said, I would suggest you go with what you deem to be the most readable.

零度℉ 2025-01-03 08:33:25

你可以在询问之前编写这样的测试代码;根据测试“使用“===”比 is_null() 快 30 倍。

http://www.php.net/manual/en/language .types.null.php#77937

You could write a test code like this before asking; according to test "Using "===" is 30x quicker than is_null()."

http://www.php.net/manual/en/language.types.null.php#77937

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