== and === in php

One reason for I hate PHP


  1. <?php   
  2.   
  3.   
  4.   
  5. if('0001' == '1'){  
  6.         echo 'when == test, 0001 and 1 are equal';  
  7. }  
  8.   
  9.   
  10. if('0001' === '1'){  
  11.         echo 'when === test, 0001 and 1 are equal';  
  12. }   
  13.   
  14. ?>   
Output:
when == test, 0001 and 1 are equal

  1. <?php   
  2.   
  3. if(1 == true){  
  4.         echo 'when == test, 1 and true are equal';  
  5. }  
  6.   
  7.   
  8. if(1 === true){  
  9.         echo 'when === test, 1 and true are equal';  
  10.   
  11. }  
  12.   
  13. ?>    
Output:
when == test, 1 and true are equal

 Note: any number equals to true, 0 equals to false.

  1. <?php   
  2.   
  3. if('any string' == true){  
  4.   
  5.         echo 'when == test, any string and true are equal';  
  6. }  
  7.   
  8. if('any string' === true){  
  9.         echo 'when === test, any string and true are equal';  
  10. }  
  11.   
  12.   
  13. ?>     

Output:
when == test, any string and true are equal

No comments:

Post a Comment