Skip to content
Advertisement

Anyone ever used PHP’s (unset) casting?

I just noticed PHP has an type casting to (unset), and I’m wondering what it could possibly be used for. It doesn’t even really unset the variable, it just casts it to NULL, which means that (unset)$anything should be exactly the same as simply writing NULL.

JavaScript

Anyone ever used it for anything? I can’t think of any possible usage for it…

Added:
Main idea of question is:
What is reason to use (unset)$smth instead of just NULL?

Advertisement

Answer

As far as I can tell, there’s really no point to using

JavaScript

over

JavaScript

The (unset)$y always evaluates to null, and unlike calling unset($y), the cast doesn’t affect $y at all.

The only difference is that using the cast will still generate an “undefined variable” notice if $y is not defined.

There’s a PHP bug about a related issue. The bug is actually about a (in my mind) misleading passage elsewhere in the documentation which says:

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

And that clearly isn’t the case.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement