The 3 different equals





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







137















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



I think using one equal sign is to declare a variable while two equal signs is for a comparison condition and lastly three equal signs is for comparing values of declared variables.










share|improve this question

























  • check out the example ... I have posted it would clarify more things ..

    – InfantPro'Aravind'
    Jan 14 '10 at 10:55






  • 1





    For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.

    – lucaferrario
    Jul 8 '13 at 14:04











  • @Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.

    – Funk Forty Niner
    Jun 11 '16 at 18:35













  • (Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.

    – Funk Forty Niner
    Jun 11 '16 at 19:09











  • I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).

    – JJJ
    May 16 '18 at 10:34


















137















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



I think using one equal sign is to declare a variable while two equal signs is for a comparison condition and lastly three equal signs is for comparing values of declared variables.










share|improve this question

























  • check out the example ... I have posted it would clarify more things ..

    – InfantPro'Aravind'
    Jan 14 '10 at 10:55






  • 1





    For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.

    – lucaferrario
    Jul 8 '13 at 14:04











  • @Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.

    – Funk Forty Niner
    Jun 11 '16 at 18:35













  • (Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.

    – Funk Forty Niner
    Jun 11 '16 at 19:09











  • I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).

    – JJJ
    May 16 '18 at 10:34














137












137








137


23






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



I think using one equal sign is to declare a variable while two equal signs is for a comparison condition and lastly three equal signs is for comparing values of declared variables.










share|improve this question
















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



I think using one equal sign is to declare a variable while two equal signs is for a comparison condition and lastly three equal signs is for comparing values of declared variables.







php comparison operators






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 16 '18 at 6:58









JJJ

29.3k147693




29.3k147693










asked Jan 14 '10 at 10:38









StrawberryStrawberry

24.3k48127182




24.3k48127182













  • check out the example ... I have posted it would clarify more things ..

    – InfantPro'Aravind'
    Jan 14 '10 at 10:55






  • 1





    For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.

    – lucaferrario
    Jul 8 '13 at 14:04











  • @Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.

    – Funk Forty Niner
    Jun 11 '16 at 18:35













  • (Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.

    – Funk Forty Niner
    Jun 11 '16 at 19:09











  • I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).

    – JJJ
    May 16 '18 at 10:34



















  • check out the example ... I have posted it would clarify more things ..

    – InfantPro'Aravind'
    Jan 14 '10 at 10:55






  • 1





    For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.

    – lucaferrario
    Jul 8 '13 at 14:04











  • @Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.

    – Funk Forty Niner
    Jun 11 '16 at 18:35













  • (Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.

    – Funk Forty Niner
    Jun 11 '16 at 19:09











  • I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).

    – JJJ
    May 16 '18 at 10:34

















check out the example ... I have posted it would clarify more things ..

– InfantPro'Aravind'
Jan 14 '10 at 10:55





check out the example ... I have posted it would clarify more things ..

– InfantPro'Aravind'
Jan 14 '10 at 10:55




1




1





For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.

– lucaferrario
Jul 8 '13 at 14:04





For advanced PHP users, knowing the difference between == and === and asking themself "is it faster to compare with == or with === when I'm sure that both the operands are the same type?", please look at my benchmark script below.

– lucaferrario
Jul 8 '13 at 14:04













@Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.

– Funk Forty Niner
Jun 11 '16 at 18:35







@Strawberry I feel the question's tag should also include the javascript tag since it also applies to that language.

– Funk Forty Niner
Jun 11 '16 at 18:35















(Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.

– Funk Forty Niner
Jun 11 '16 at 19:09





(Update): I have added the javascript tag in an edit, since there have been quite a few questions where people were using = in javascript. That doesn't only apply to PHP but other languages also.

– Funk Forty Niner
Jun 11 '16 at 19:09













I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).

– JJJ
May 16 '18 at 10:34





I went ahead and deleted the JavaScript tag: all answers here are only about PHP (except the latest which is very weak), there are differences between how the languages handle it, and there is already a similar de facto canonical JavaScript question (Which equals operator (== vs ===) should be used in JavaScript comparisons?).

– JJJ
May 16 '18 at 10:34












5 Answers
5






active

oldest

votes


















164














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.






share|improve this answer


























  • when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

    – stloc
    Jan 8 '16 at 9:20






  • 1





    @stloc, === with objects tells you if they are the same object, not if they have the same content.

    – Andrea
    Jan 27 '16 at 20:22











  • Introduced in PHP4, funny in Y2018 ;-)

    – Mvorisek
    Jul 13 '18 at 15:36



















22
















  • = 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).






share|improve this answer































    7














    = 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






    share|improve this answer



















    • 1





      Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

      – Phil Perry
      Aug 21 '13 at 16:57



















    1














    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.






    share|improve this answer





















    • 8





      Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

      – gnarf
      Nov 12 '13 at 5:00











    • My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

      – lucaferrario
      Feb 18 '16 at 15:40



















    0














    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






    share|improve this answer






















      protected by Rizier123 Jun 18 '15 at 16:50



      Thank you for your interest in this question.
      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



      Would you like to answer one of these unanswered questions instead?














      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      164














      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.






      share|improve this answer


























      • when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

        – stloc
        Jan 8 '16 at 9:20






      • 1





        @stloc, === with objects tells you if they are the same object, not if they have the same content.

        – Andrea
        Jan 27 '16 at 20:22











      • Introduced in PHP4, funny in Y2018 ;-)

        – Mvorisek
        Jul 13 '18 at 15:36
















      164














      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.






      share|improve this answer


























      • when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

        – stloc
        Jan 8 '16 at 9:20






      • 1





        @stloc, === with objects tells you if they are the same object, not if they have the same content.

        – Andrea
        Jan 27 '16 at 20:22











      • Introduced in PHP4, funny in Y2018 ;-)

        – Mvorisek
        Jul 13 '18 at 15:36














      164












      164








      164







      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 16 '16 at 14:26









      MC Emperor

      9,342125690




      9,342125690










      answered Jan 14 '10 at 10:43









      gnarfgnarf

      91.7k19116156




      91.7k19116156













      • when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

        – stloc
        Jan 8 '16 at 9:20






      • 1





        @stloc, === with objects tells you if they are the same object, not if they have the same content.

        – Andrea
        Jan 27 '16 at 20:22











      • Introduced in PHP4, funny in Y2018 ;-)

        – Mvorisek
        Jul 13 '18 at 15:36



















      • when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

        – stloc
        Jan 8 '16 at 9:20






      • 1





        @stloc, === with objects tells you if they are the same object, not if they have the same content.

        – Andrea
        Jan 27 '16 at 20:22











      • Introduced in PHP4, funny in Y2018 ;-)

        – Mvorisek
        Jul 13 '18 at 15:36

















      when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

      – stloc
      Jan 8 '16 at 9:20





      when I compare two identical datetime, I have false result, why ? example : sandbox.onlinephpfunctions.com/code/…

      – stloc
      Jan 8 '16 at 9:20




      1




      1





      @stloc, === with objects tells you if they are the same object, not if they have the same content.

      – Andrea
      Jan 27 '16 at 20:22





      @stloc, === with objects tells you if they are the same object, not if they have the same content.

      – Andrea
      Jan 27 '16 at 20:22













      Introduced in PHP4, funny in Y2018 ;-)

      – Mvorisek
      Jul 13 '18 at 15:36





      Introduced in PHP4, funny in Y2018 ;-)

      – Mvorisek
      Jul 13 '18 at 15:36













      22
















      • = 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).






      share|improve this answer




























        22
















        • = 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).






        share|improve this answer


























          22












          22








          22









          • = 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).






          share|improve this answer















          • = 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).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 14 '10 at 10:44









          Rich AdamsRich Adams

          21.1k43058




          21.1k43058























              7














              = 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






              share|improve this answer



















              • 1





                Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

                – Phil Perry
                Aug 21 '13 at 16:57
















              7














              = 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






              share|improve this answer



















              • 1





                Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

                – Phil Perry
                Aug 21 '13 at 16:57














              7












              7








              7







              = 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






              share|improve this answer













              = 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







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 14 '10 at 10:44









              Silvio DonniniSilvio Donnini

              2,47612329




              2,47612329








              • 1





                Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

                – Phil Perry
                Aug 21 '13 at 16:57














              • 1





                Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

                – Phil Perry
                Aug 21 '13 at 16:57








              1




              1





              Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

              – Phil Perry
              Aug 21 '13 at 16:57





              Also know about != and !== 'not' versions of the two comparison operators. Some languages use := as assignment, just to avoid this kind of confusion.

              – Phil Perry
              Aug 21 '13 at 16:57











              1














              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.






              share|improve this answer





















              • 8





                Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

                – gnarf
                Nov 12 '13 at 5:00











              • My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

                – lucaferrario
                Feb 18 '16 at 15:40
















              1














              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.






              share|improve this answer





















              • 8





                Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

                – gnarf
                Nov 12 '13 at 5:00











              • My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

                – lucaferrario
                Feb 18 '16 at 15:40














              1












              1








              1







              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.






              share|improve this answer















              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 1 '16 at 15:20









              Peter Mortensen

              13.9k1987114




              13.9k1987114










              answered Jul 8 '13 at 14:03









              lucaferrariolucaferrario

              83289




              83289








              • 8





                Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

                – gnarf
                Nov 12 '13 at 5:00











              • My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

                – lucaferrario
                Feb 18 '16 at 15:40














              • 8





                Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

                – gnarf
                Nov 12 '13 at 5:00











              • My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

                – lucaferrario
                Feb 18 '16 at 15:40








              8




              8





              Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

              – gnarf
              Nov 12 '13 at 5:00





              Microbenchmarks like this are not very reliable. It is also highly unlikely that you'd ever want to worry about == or === as the cause of your performance problem. IMO: It's better to be strict (===) unless you explicitly want to be loose (==) about your comparisons. The number of strange edge cases i.e. "5 is not a number" == 5 can lead to wacky bugs. === never suffers from this problem.

              – gnarf
              Nov 12 '13 at 5:00













              My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

              – lucaferrario
              Feb 18 '16 at 15:40





              My test was to tell programmers that if they're choosing === for performance reasons, they're wrong. So programmers are free to choose === or == based on logical reasons but not on performance reasons: there are different cases to prefer the one or the other and performance must not be taken into account at all.

              – lucaferrario
              Feb 18 '16 at 15:40











              0














              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






              share|improve this answer




























                0














                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






                share|improve this answer


























                  0












                  0








                  0







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 25 at 13:12









                  GideonGideon

                  11615




                  11615

















                      protected by Rizier123 Jun 18 '15 at 16:50



                      Thank you for your interest in this question.
                      Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                      Would you like to answer one of these unanswered questions instead?



                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents