php judges 0 and null through various functions

  • 2021-10-13 06:46:03
  • OfStack

Judgment of 0 by function


  $cast_id = 0;
  var_dump(strlen($cast_id)); //1
  var_dump(empty($cast_id)); // true
  var_dump(isset($cast_id)); //true
  var_dump(is_null($cast_id));//false

Judgment of emptiness


  $cast_id = "";
  var_dump(strlen($cast_id)); //0
  var_dump(empty($cast_id)); // true
  var_dump(isset($cast_id)); //true
  var_dump(is_null($cast_id));//false

Add: Let's introduce the solution that 0 does not mean that null is empty in php syntax

Today, I encountered such a problem as this: In the php statement, I want to judge a value greater than or equal to 0. I use ( $value !=null && $value >=0 ), and the result returned is empty, which is really strange.

Experimental summary:

The php statement is as follows:


$index=0;
echo "A: ".$index."<br>"; //0
echo "B: ".($index !=null && $index >=0)."<br>";//
echo "C: ".(isset($index) && $index >=0)."<br>";//1
echo "D: ".(0 !=null)."<br>";//

Results:

A: 0
B:
C: 1
D:

To determine that a numeric value [the array may be empty, etc.] is greater than or equal to 0, there is another method:   is_numeric($index) === true


$index=array_search($url, $contentOtherStr, true);
 // Value greater than or equal to 0 ,   Existence 
if(is_numeric($index) === true)
 {
echo "$url existed. "."<br>";
 }else{
echo "$url Add. "."<br>";
array_push($contentOtherStr, $url);
 }

This is very strange, and it has finally been solved. Under Mark 1.

Summary: The statement of php is a little strange. Students transferred from other programming languages should be more careful, pay attention to inertial thinking and grammatical differences, and avoid falling into a pit.

Other information:

The reason is that in PHP, variables are stored in the structure of C language, and empty strings, NULL and false are stored with the value of 0, in which this structure has an zend_uchar type; Such a member variable is used to hold the type of the variable, and the type of the empty string is string, the type of NULL is NULL, and the type of false is boolean.

This 1 point can be used echo gettype(''); And echo gettype(NULL); Let's print it! The = = = operator compares not only values but also types, so the third one is false!

In addition, in php,

= 1 equal sign is an assignment
= = Two equal signs are used to judge equality and only compare values, not types
= = = 3 equal signs are used to judge that both values and types are equal
! = not equal to symbol, only compare values, regardless of type
! = = Incomplete equal sign, compare value and type

So empty strings (''), false, NULL and 0 are equal in value but not of type 1!

Note:

NULL is a special type.

NULL in both cases

1. $var = NULL;
2. $var;
3. "", "0," 0 ", NULL, FALSE, array (), var $var; And objects without any attributes are considered empty, and TRUE is returned if var is empty.

Distinguish between 0 and null in PHP


function test()
{
  $a = 0;
  $b = '';
  $c = null;

  //  Differentiate  0 , '' , null

  // 1 , method 1
  if ($a !== '') {
    echo '0 And null';
  } else {
    echo ' Empty string ';
  }

  // 2 , method 2
  if ($a !== null) {
    echo '0 And empty strings ';
  } else {
    echo 'null';
  }

  // 3 , method 3
  if (strlen($a) > 0) {
    echo '0';
  } else {
    echo ' Empty string and null';
  }

  //  Above 3 All kinds of methods can only put one of them 1 Distinguish one from the other two, if necessary 3 A separate distinction needs to be used in combination 
}

Related articles: