|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55164 Unclear/incorrect description of "casting to null"
Submitted: 2011-07-08 21:18 UTC Modified: 2012-01-12 05:22 UTC
Avg. Score:3.3 ± 0.9
Reproduced:5 of 6 (83.3%)
Same Version:3 (60.0%)
Same OS:2 (40.0%)
From: deceze at gmail dot com Assigned: frozenfire (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2011-07-08 21:18 UTC] deceze at gmail dot com
The documentation at states:

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

This seems incorrect and/or misleading. "Casting" to `null` is not possible as 
such through the cast syntax `(null)`. Setting the type of a variable using 
`settype($foo, 'null')` or assigning `null` to any variable will not "remove" the 
variable. The variable continues to exist with the value `null`.

Test script:
// the documentation suggests something like this:

$foo = 'bar';
settype($foo, 'null');
echo $foo; // PHP Notice: Undefined variable: foo

// actual behavior:

$foo = 'bar';
settype($foo, 'null');
echo $foo; // empty output, no warning


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-08 21:35 UTC]
-Status: Open +Status: Analyzed
 [2011-07-08 21:35 UTC]
I agree there is a bug here, but you can cast something to null using (unset) 

$a = "foo";
$b = "bar";
$b = (unset) $a;


Slightly odd that it's (unset) and not (null) but it is what it is. That section 
should refer to the 
(unset) documentation within the type juggling documentation.
 [2011-07-08 21:40 UTC] deceze at gmail dot com
`$b = (unset)$a` in itself is quite weird, since it doesn't `unset` either `$a` or 
`$b`, it just assigns `null` to `$b`.
 [2011-07-08 21:47 UTC]
Simply this:
Outputs NULL.
 [2011-07-08 21:53 UTC] deceze at gmail dot com
Yes, but:



  $foo = 'bar';
  $foo = (unset)$foo;  // or $foo = null



  Notice: Undefined variable: undefined

  Notice: Undefined variable: foo

`null` is the default value for undefined variables, but undefined/"removed" 
variables are not the same as variables with a `null` value.
 [2012-01-12 04:21 UTC] frozenfire at thefrozenfire dot com
I believe the major distinction between the various states of the variable has to 
do not with the value, but rather its presence in the symbol table. When you 
assign null to a variable, it is still in the symbol table. When you cast it to 
unset, it's still in the symbol table until the operation finishes. When you 
unset it, it's removed from the symbol table, thus becoming "undefined".

I'll see about confirming my suspicions through inspection of the source.
 [2012-01-12 05:07 UTC]
Rather, it seems that the confusion here is with what (unset) casting does. It's 
not meant to unset a variable, but rather to cast it to null. That casting type 
exists purely for completeness, and you may as well just use NULL.

The way to undefine a variable is through the language construct unset. Perhaps 
I'll make a note in the unset() function reference that it's unrelated to unset-
casting, and remove that errant example.
 [2012-01-12 05:22 UTC]
Automatic comment from SVN on behalf of frozenfire
Log: Clarified highly-misleading (unset) casting example. Resolves bug #55164.
 [2012-01-12 05:22 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

 [2012-01-12 05:22 UTC]
-Status: Analyzed +Status: Closed -Assigned To: +Assigned To: frozenfire
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Feb 21 07:01:34 2024 UTC