php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76308 reference to unset variable
Submitted: 2018-05-07 07:49 UTC Modified: 2018-05-07 08:21 UTC
From: bichinhoverde at spwinternet dot com dot br Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.2.5 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bichinhoverde at spwinternet dot com dot br
New email:
PHP Version: OS:

 

 [2018-05-07 07:49 UTC] bichinhoverde at spwinternet dot com dot br
Description:
------------
Creating a reference to an unset variable does not generate a warning/notice. Also, variable changes after the reference is created.

Test script:
---------------
<?php

error_reporting(E_ALL);

$c =& $a['b'];

echo count($a);


Expected result:
----------------
1. A warning/notice should be generated for referencing an unset variable.
2. Variable should not change. count($a) should return zero and cause warning/notice.

Actual result:
--------------
1. No warning/notice.
2. count($a) return 1.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-07 08:05 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-05-07 08:05 UTC] requinix@php.net
A reference to a variable or array/key can only happen if the source exists. If it does not then what would the reference be to? However if $a[b] does not exist then what would happen to $c? What would it be a reference to?
So PHP must create $a[b] automatically. Doing so also requires creating the $a array, since that doesn't exist yet either, but just like during $a[b]=123 PHP does not warn when it happens automatically.

So
1a. There is no warning for implicitly creating arrays. This is not related to references.
1b. There is no warning when $a[b] was implicitly created because you did, after all, want a reference to it.
2. Since $a[b] had to be created, count($a) will return 1.

This is also mentioned in the docs.
http://php.net/manual/en/language.references.whatdo.php
> Note: If you assign, pass, or return an undefined variable by reference, it will get created.
 [2018-05-07 08:21 UTC] bichinhoverde at spwinternet dot com dot br
This is a strange behavior.
If I do $c = $a['b']; I get a notice for trying to get the value of an undefined variable. But $c =& $a['b']; silently creates the variable.
I don't mind the array being created implicitly, but a notice should be generated since I am referencing something that does not exist.
Trying to get the value of something undefined: notice.
Trying to get the reference of something undefined: perfectly fine.
 [2018-05-07 08:23 UTC] spam2 at rhsoft dot net
yes because otherwise functions like http://php.net/manual/en/function.exec.php would not be possible (or do you create $output and $return_var in your code  before? hint: strip that useless lines..)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC