Home  >  Article  >  Backend Development  >  In-depth understanding of the relationship between 0, null, empty, empty, false, and strings in PHP

In-depth understanding of the relationship between 0, null, empty, empty, false, and strings in PHP

WBOY
WBOYOriginal
2016-07-25 08:58:55818browse
  1. //Judge the relationship between 0 and '' and empty null false start//

  2. if('safdasefasefasf'==0) {

  3. echo "The string is converted into a number equal to 0
    ";
  4. }//output: The string is converted into a number equal to zero.

  5. This is a key example

  6. The manual explains: The value is determined by the first part of the string. If the string begins with legal numeric data, that number is used as its value, otherwise its value is 0 (zero).

  7. In other words, '3asfdf'==3 ; 'adsfasdf'==0 Be very careful

  8. $a=0;

  9. if($a= =''){
  10. echo "0 is equal to ''
    ";
  11. } //output:0 is equal to ''
  12. if(trim($a)==''){
  13. echo "trim(0 ) is equal to ''
    ";
  14. } //no output

  15. if($a===''){

  16. echo "0===''
    ";
  17. } //no output
  18. if(empty($a)){
  19. echo "'' is empty
    ";
  20. } //output:'' is empty
  21. if(is_null ($a)){
  22. echo "0 is null
    ";
  23. } //no output
  24. if(is_numeric($a)){
  25. echo "0 is numeric
    ";
  26. } //output:0 is numeric
  27. if(is_string($a)){
  28. echo "0 is string
    ";
  29. } //no output
  30. if(strval($a)==' '){
  31. echo "0 converted into a string is ''
    ";
  32. } //no output
  33. // Determine the relationship between 0 and '' and empty null false end //
  34. // Determine the relationship between '' and 0 and empty null false start //

  35. $b = '';

  36. if($b==0){
  37. echo "'' is equal to 0
    ";
  38. } //output:'' is equal to 0
  39. if(!''){
  40. echo "'' is false
    ";
  41. } // output:'' is false
  42. if(!0){
  43. echo "0 is false
    ";
  44. } //output:0 is false bbs.it-home.org
  45. // Judge '' and The relationship between 0 and empty null falseend //

  46. echo "Be careful when judging empty (''), 0 is also equivalent to '', 0 and '' are both equivalent to empty Characters and false, it is best to use ===";

  47. ?>

Copy the code

Output results: 0 is equal to " " is empty 0 is numeric " is equal to 0 " which is false. 0 is false. Be careful when judging empty ("). 0 is also equivalent to ". 0 and " are both equivalent to empty characters and false, and are judged to be empty. It is best to use === and can only be explained this way: 0 is also equivalent to ", 0 and " are equivalent to the null character and false. Be careful when judging empty ("). 0 is also equivalent to ", 0 and " are equivalent to empty characters and false. It is best to use === when judging empty;

echo 0 == null; echo '**
' ; //true echo 0 === null; echo '**
' ; //false echo (string)0 != null; echo '**
' ; //true echo 0 != null; echo '**
' ; //false echo 0 !== null; echo '**
' ; //true


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn