Assigning a NULL variable in PHP: what does it do?

When saving code, I encounter loops where at the end of the loop several variables are set to NULL as follows: $var = NULL; . From what I understand in the manual, NULL is mainly used as something that can be compared to PHP code. Since NULL not a type and is not a string or a number, outputting it does not make sense.

Unfortunately, I cannot give an example, but I think that NULL values ​​are written to a file in our code. My question is: does $var matter after the assignment, and will the echo / record output the result?

EDIT: I read the PHP manual for NULL . There is no need to post this: http://php.net/manual/en/language.types.null.php in the comments or answers, or from top to bottom so as not to have RTM. Thanks!

+8
null php
source share
6 answers
 [ghoti@pc ~]$ php -r '$i="foo"; print "ONE\n"; var_dump($i); unset($i); print "TWO\n"; var_dump($i); $i=NULL; print "THREE\n"; var_dump($i); print "\n"; if (isset($i)) print "Set.\n"; if (is_null($i)) print "is_null\n";' ONE string(3) "foo" TWO NULL THREE NULL is_null [ghoti@pc ~]$ 

The result of isset() will be boolean, but the variable is still defined. The isset() function will be better called isnotnull() . :-P

Note that is_null() will also return true for a value that has never been set.

Yay PHP.

+3
source share

null is pretty much like any other value in PHP (actually it is also a different data type than string, int, etc.).

However, there is one important difference: isset($var) checks for the existence of var and has a nonzero value.

If you plan to read the variable again before assigning a new value, unset() is the wrong way, but assigning null perfectly fine:

 php > $a = null; php > if($a) echo 'x'; php > unset($a); php > if($a) echo 'x'; Notice: Undefined variable: a in php shell code on line 1 php > 

As you can see, unset() actually removes the variable, as it never existed, while assigning null sets it to a specific value (and creates a variable if necessary).

A useful example of using null is the default arguments when you want to know if it was provided or not, and empty lines, zero, etc. also valid:

 function foo($bar = null) { if($bar === null) { ... } } 
+1
source share

A variable can be set to NULL to indicate that it does not contain a value. It makes sense if at some later point in the code the variable is checked for NULL .

The variable can be explicitly set to NULL to free the memory used by it. This makes sense if the variable consumes a lot of memory (see this question ).

Run the code dry and you could figure out the exact reason.

+1
source share

Null in PHP means no value is assigned to a variable.

http://php.net/manual/en/language.types.null.php

0
source share

It seems that the goal of a zero implementation based on information is to clear the variable.

You can disable a variable in PHP by setting it to NULL or using the unset () function.

unset () destroys the specified variables.

The behavior of the unset () function inside a function may vary depending on what type of variable you are trying to destroy.

If the global variable is disabled () inside the function, the local variable is destroyed. The variable in the calling environment will retain the same value as before unset ().

0
source share

Null is a special data type that can have only one value, which itself is. In other words, null is not only a data type, but also a literal character of a keyword, a data type variable null is a variable that does not have a value assigned to it when creating a variable without a value, which is automatically set to null, so this is all rubbish in this the memory location is cleared before, otherwise the program may try to process it.

-one
source share

All Articles