3种不同的比较,=,==,===

发布于 2025-02-11 00:13:58 字数 167 浏览 2 评论 0原文

======之间有什么区别?

我认为使用一个=是声明一个变量,而==是用于比较条件,最后=====是用于比较声明的值变量。

What is the difference between =, ==, and ===?

I think using one = is to declare a variable while == is for a comparison condition and lastly === is for comparing values of declared variables.

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

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

发布评论

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

评论(5

为你鎻心 2025-02-18 00:13:58

您有= 分配运算符== '等于'比较操作员================== “相同”比较操作员

$a = $b    Assign     Sets $a to be equal to $b.
$a == $b   Equal      TRUE if $a is equal to $b.
$a === $b  Identical  TRUE if $a is equal to $b, and they are of the same type.
                      (introduced in PHP 4)

有关======的更多信息,以及要使用的情况,请查看文档

You have = the assignment operator, == the 'equal' comparison operator and === the 'identical' comparison operator.

$a = $b    Assign     Sets $a to be equal to $b.
$a == $b   Equal      TRUE if $a is equal to $b.
$a === $b  Identical  TRUE if $a is equal to $b, and they are of the same type.
                      (introduced in PHP 4)

For more info on the need for == and ===, and situations to use each, look at the docs.

命硬 2025-02-18 00:13:58
  • =是分配操作员
  • ==
    是比较操作员(检查是否
    两个变量具有相等的值)
  • ===是相同的比较
    操作员(检查两个变量是否
    具有相等的值,相同
    类型)。
  • = is the assignment operator
  • ==
    is the comparison operator (checks if
    two variables have equal values)
  • === is the identical comparison
    operator (checks if two variables
    have equal values and are of the same
    type).
绾颜 2025-02-18 00:13:58

=分配运算符

==检查两个变量是否具有相同的值

===检查两个变量是否具有相同的值,并且它们的类型是否相同

= assignment operator

== checks if two variables have the same value

=== checks if two variables have the same value AND if their types are the same

伴随着你 2025-02-18 00:13:58

=操作员将值分配给变量
$六= 6;值6分配给变量$六

==运算符检查两个变量的值是否相等,并且主要在诸如if语句

$a = 2;
$b = 2;
if ($a == $b) { 
    echo both variables have the same value; 
}

===运算符等条件下使用,类似于==(检查值等于值),并且还检查两个是否相同数据类型

$a = 2;
$b = "2";
if ($a === $b) {
    echo "both variable have same value and of same data type";
} else {
    echo 'both variable is either not equal or not of same data type';
}

//此处$ a是类型int类型,而$ b是字符串类型。所以在这里输出

The = operator assigns the value to a variable
$six = 6; value 6 is assigned to variable $six

== operator check if the value of both variables is equal and mostly used in conditions like if statements

$a = 2;
$b = 2;
if ($a == $b) { 
    echo both variables have the same value; 
}

=== operator similar to == (check if the value equals) and also check if both of same data type

$a = 2;
$b = "2";
if ($a === $b) {
    echo "both variable have same value and of same data type";
} else {
    echo 'both variable is either not equal or not of same data type';
}

// here $a is of type int whereas $b is of type string. So here the output

江湖彼岸 2025-02-18 00:13:58

对于高级PHP用户,了解=======之间的差异,并且“与==进行比较速度更快? ===当我确定两个操作数都是相同类型时吗?”

简短而一般的答案是:在这种情况下,使用===没有性能提高,因此您可能应该使用==

对于有兴趣对其进行基准测试的人,您可以使用以下代码我编写了临时的代码,并尝试$ a$ b $ a :

<?php
    // CONFIGURATION
    $cycles = 1000000;
    $a = 'random string 1';
    $b = 'random string 2';

    // FUNCTIONS
    function compare_two_equals($a, $b) {
        if ($a == $b) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function compare_three_equals($a, $b) {
        if ($a === $b) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // EXECUTION
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_two_equals($a, $b);
    }
    $time_two_a = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_three_equals($a, $b);
    }
    $time_three_a = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_two_equals($a, $b);
    }
    $time_two_b = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_three_equals($a, $b);
    }
    $time_three_b = microtime(TRUE) - $time;
    $time = microtime(TRUE);

    // RESULTS PRINTING
    print "<br />\nCOMPARE == (FIRST TRY): " . number_format($time_two_a, 3) . " seconds";
    print "<br />\nCOMPARE == (SECOND TRY): " . number_format($time_two_b, 3) . " seconds";
    print "<br />\nCOMPARE === (FIRST TRY): " . number_format($time_three_a, 3) . " seconds";
    print "<br />\nCOMPARE === (SECOND TRY): " . number_format($time_three_b, 3) . " seconds";
?>

注意:比较:仅当每个“第一次尝试”都非常接近其“第二次尝试”时有效。如果它们有很大的不同,则意味着处理器在执行比较时忙于做其他事情,因此结果不可靠,并且应该再次运行基准。

For advanced PHP users, knowing the difference between ==and === and asking themselves "is it faster to compare with == or with === when I'm sure that both the operands are the same type?"

The short and general answer is: There is no performance gain in using === in this cases, so you should probably use ==.

For the ones interested in benchmarking it themselves, you can use the following code I wrote ad-hoc and try different values for $a and $b:

<?php
    // CONFIGURATION
    $cycles = 1000000;
    $a = 'random string 1';
    $b = 'random string 2';

    // FUNCTIONS
    function compare_two_equals($a, $b) {
        if ($a == $b) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function compare_three_equals($a, $b) {
        if ($a === $b) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    // EXECUTION
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_two_equals($a, $b);
    }
    $time_two_a = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_three_equals($a, $b);
    }
    $time_three_a = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_two_equals($a, $b);
    }
    $time_two_b = microtime(TRUE) - $time;
    $time = microtime(TRUE);
    for ($count_a = 0; $count_a < $cycles; $count_a++) {
        compare_three_equals($a, $b);
    }
    $time_three_b = microtime(TRUE) - $time;
    $time = microtime(TRUE);

    // RESULTS PRINTING
    print "<br />\nCOMPARE == (FIRST TRY): " . number_format($time_two_a, 3) . " seconds";
    print "<br />\nCOMPARE == (SECOND TRY): " . number_format($time_two_b, 3) . " seconds";
    print "<br />\nCOMPARE === (FIRST TRY): " . number_format($time_three_a, 3) . " seconds";
    print "<br />\nCOMPARE === (SECOND TRY): " . number_format($time_three_b, 3) . " seconds";
?>

NOTE: The comparison is valid only when each "FIRST TRY" is very close to its "SECOND TRY". If they are significantly different, it means that the processor was busy doing something else while executing the comparisons and so the results are unreliable and the benchmark should be run again.

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