Php reset array to null

How to reset a variable to NULL in PHP?

Question: In maintaining code, I’m encountering loops, where at the end of the loop several variables are set to like so: . From what I understand in the manual, NULL is meant mostly as something to compare against in PHP code.

How to reset a variable to NULL in PHP?

I can use isset($var) to check if the variable is not defined or null. (eg. checking if a session variable has been set before)

But after setting a variable, how do I reset it to the default such that isset($var) returns false ?

As nacmartin said, unset will «undefine» a variable. You could also set the variable to null, however this is how the two approaches differ:

$x = 3; $y = 4; isset($x); // true; isset($y); // true; $x = null; unset($y); isset($x); // false isset($y); // false echo $x; // null echo $y; // PHP Notice (y not defined) 

While a variable can be null or not null, a variable can also be said to be set or not set.

to set a variable to null, you simply

This will make $var null , equivalent to false , 0 and so on. You will still be able to get the variable from $GLOBALS[‘var’] since it is still defined / set. However, to remove a variable from the global and/or local namespace, you use the

Читайте также:  Java integer and null

This will make $var not set at all. You won’t be able to find in $GLOBALS .

Also, you can set the variable to null :

Php — Using reference to nonexistent value sets variable, When passing a non-existent value by reference, PHP creates the value and sets it to NULL. I noticed it when memory increases were occurring …

Assigning NULL to a variable in PHP: what does that do?

In maintaining code, I’m encountering loops , where at the end of the loop several variables are set to NULL like so: $var = NULL; . From what I understand in the manual, NULL is meant mostly as something to compare against in PHP code. Since NULL has no type and is not a string or number, outputting it makes no sense.

I unfortunately cannot provide an example, but I think the NULL values are being written to a file in our code. My question is: does $var have a value after the assignment, and will echoing/writing it produce output?

EDIT: I have read the PHP manual entry on NULL . There is no need to post this: http://php.net/manual/en/language.types.null.php in a comment or answer, or top downvote me for not having RTM. Thank you!

[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 false, but the variable is still defined. The isset() function would be better named isnotnull() . 😛

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

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

However, there is one important difference: isset($var) checks for the var to exist and have a non-null value.

If you plan to read the variable ever again before assigning a new value, unset() is the wrong way to do but assigning null is 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 deletes the variable, just like it never existed, while assigning null sets it to a specific value (and creates the variable if necessary).

A useful use-case of null is in default arguments when you want to know if it was provided or not and empty strings, zero, etc. are valid, too:

Null in PHP means a variable were no value was assigned.

A variable could 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 being NULL .

A variable might be explicitly set to NULL to release memory used by it. This makes sense if the variable consumes lots of memory (see this question).

Dry run the code and you might be able to figure out the exact reason.

Php — How to set a value in mysql to NULL using a, $eloleg = ‘NULL’; mysqli_query($connection, «UPDATE $table SET eloleg=$eloleg WHERE date=’$eredeti_date'»); But as others have said, you should …

Set local variable to null in PHP

Very often in code added by my more .NET oriented colleagues, I’ll run into something like this:

Is there any benefit to setting $localVariable to null? Since it’s a local variable (and therefore will run out of scope anyway), I would assume not, but please do correct me if I’m wrong.

Your assumption would be correct for both php and .NET.

There are two things to consider here: memory management and defensive coding.

For memory management purposes, setting things to null is pretty much pointless. The garbage collector can figure things out just fine, and everything will be fine once the function exits. For smaller pieces of memory (like an integer) this just clutters up the code.

HOWEVER, for defensive coding purposes , this might be ok. This example is just silly, but sometimes in your code it’s a good idea to set things to null when you’re done using them, so that if they DO get used later on in the function, it blows up in the programmers face (and is immediately obvious) rather than leading to bugs that are hard to track down.

No, it could be useful only if there’s more heavy processing after it in order to free resources. But unset() would be more useful in that case anyway.

As CG says, you are correct in assuming this is unnecessary.

However, it MAY be more relevant if more memory intensive code comes afterward, like in this function:

function someFunction() < $localVariable = new MemoryPiggy(); $number = $localVariable->calcValue(); $localVariable = null; $localVariable2 = new AnotherMemoryHog($number); /* * Do stuff with $localVariable2 */ return $ret; > 

I think this is why most people would prematurely force a variable out of scope: to allow the garbage collector to reclaim that space if it wants to, knowing that you won’t be using it.

P.S. This is an example where the next part of the code also gobbles up memory. Another example might be where the 2nd part of the code takes a long, long time to execute. Barring situations like these, though, there is no need to increase the complexity of your program by nulling out locals.

How to Insert NULL Value from PHP Variable to MySQL, I tried the same thing that you did- set the variable to NULL, null, ‘NULL’, «NULL», even wrote null as a constant in the SQL string- nothing. The field …

What does a variable set to «&NULL» means in PHP?

I’m working on a tree structure in PHP. When looping through the nodes, an exception is sometime thrown because some node that should not be null is null, more precisely it’s set to «&NULL»:

array(13) < // . // some data. // . ["segments"]=>NULL ["leaf"]=> bool(false) ["children"]=> &NULL > 

Since it’s not inside quotes, I assume it’s some kind of special value, but what does it mean?

It just means that it is a reference to a value NULL

$a = array(); $n = null; $a[1] =& $n; var_dump($a); // array(1) < [1]=>&NULL > 

If you change $n = null; to $n = 1; — then you’ll get &int(1)

It’s a reference to a value that is null. «&» is the reference symbol.

Session — PHP Variables get overwritten to Null, I’m having issues maintaining variables in the Session. I’ve confirmed that the same Session ID persists through page navigation, and I’ve …

Источник

How to set array key to null if it does not exist?

UPDATE I need this because i need to encode array in JSON and push to browser. The problem is that the json_encode sets key to 0 if it not exists, but if there is NULL it also remains the same in browser. So when i using this array in JS, i can detect where is the real 0 and where 0 was created because there was no key.

You can’t have NULL as a key, nor can you have duplicate array keys. Why not set the value to NULL ? (ps: array(‘a’=> ‘value’, ‘b’) will create: array(‘a’=> ‘value’, 0 => ‘b’) ) There is no such thing as a «non-existent» key. What exactly are you trying to do?

From the documentation Null will be cast to the empty string, i.e. the key null will actually be stored under «».

@dbf exactly. This means that you set it to «», even when you can put NULL into it. I say there can’t be NULL keys, when, after inserting are still NULL’s visible.

2 Answers 2

The keys do exist, they are just automatically assigned and are numeric. From Arrays

The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.

You cannot detect, if the numeric key was assigned explicitly or if the key was just omitted. You can do this only at the time when the array is created or the value is appended to the array. Afterwards, you can only replace numeric keys with some other value.

If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

This means, you cannot replace missing keys with NULL , but only one of them. If you set multiple keys to NULL , every assignment will delete the previous NULL key/value pair.

Источник

Оцените статью