Doc #55164 [Ana]: Unclear/incorrect description of "casting to null"
| From: | deceze at gmail dot com | Date: | Sat, 09 Jul 2011 01:53:07 +0000 |
| Subject: | Doc #55164 [Ana]: Unclear/incorrect description of "casting to null" | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-6853@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55164&edit=1
ID: 55164
User updated by: deceze at gmail dot com
Reported by: deceze at gmail dot com
Summary: Unclear/incorrect description of "casting to null"
Status: Analyzed
Type: Documentation Problem
Package: Documentation problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Yes, but:
error_reporting(E_ALL);
var_dump($undefined);
$foo = 'bar';
$foo = (unset)$foo; // or $foo = null
var_dump($foo);
unset($foo);
var_dump($foo);
Outputs:
Notice: Undefined variable: undefined
NULL
NULL
Notice: Undefined variable: foo
NULL
null is the default value for undefined variables, but undefined/"removed"
variables are not the same as variables with a null value.
Previous Comments:
------------------------------------------------------------------------
[2011-07-08 21:47:26] philip@php.net
Simply this:
<?php
var_dump($undefined);
?>
Outputs NULL.
------------------------------------------------------------------------
[2011-07-08 21:40:14] 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:35:45] philip@php.net
I agree there is a bug here, but you can cast something to null using (unset)
so:
$a = "foo";
$b = "bar";
$b = (unset) $a;
var_dump($b);
NULL
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:18:01] deceze at gmail dot com
Description:
------------
The documentation at http://www.php.net/manual/en/language.types.null.php
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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55164&edit=1