Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Cast variable to null

Tags:

php

null

casting

I just stumbled upon a thing in the PHP manual that is new to me...

Casting a variable to null will remove the variable and unset its value. (Source)

Now I wonder how this can be done... I tried (null) and (NULL) but it seems to be interpreted as the value null, not the type null. I know this question must sound ridiculous, but does somebody know how to cast to null?

like image 827
Paul Avatar asked Jan 19 '11 14:01

Paul


1 Answers

Update: Since posting this 9 years ago, casting to unset has changed behavior and is deprecated. The last defined behavior was equivalent to setting the variable equal to NULL. You can either set the variable equal to NULL to retain the variable but indicate it has no value, or you can call the unset function to remove it entirely.

Original Answer:

$a = (unset) $a;

See type casting.

like image 52
Brandon Horsley Avatar answered Sep 22 '22 11:09

Brandon Horsley