php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #25826 undefined notices via unset()
Submitted: 2003-10-10 15:01 UTC Modified: 2013-10-27 15:22 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:0 of 1 (0.0%)
From: danielc at analysisandsolutions dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2003-10-10 15:01 UTC] danielc at analysisandsolutions dot com
Description:
------------
unset() acts inconsistently when acting upon array keys that don't exist.

In some cases it throws Notices about "Undefined index" "Undefined variable" and "Undefined property."

The behavior varies depending on
   a) the dimension of the array trying to be unset
   b) whether the array is set
   c) if the unset() call is in a class
   d) the version of PHP being used

Reproduce code:
---------------
<?php

class test {

    var $Array2 = array();

	function test() {
        unset($this->Nada);                 // no problem

        unset($this->Array1['one:one']);    // 4.3.3:  Undefined property
                                            // 500b2:  no problem
		unset($this->Array1['one:two'][1]); // 4.3.3:  Undefined property
                                            // 500b2:  no problem

		unset($this->Array2['two:one']);    // no problem
		unset($this->Array2['two:two'][1]); // Undefined index

        unset($Nada);                       // no problem

        unset($Array3['three:one']);        // Undefined variable
        unset($Array3['three:two'][1]);     // Undefined variable

        $Array4 = array();
        unset($Array4['four:one']);         // no problem
        unset($Array4['four:two'][1]);      // Undefined index
    }
}

$test1 = new test();

unset($Nada);                               // no problem

unset($Array5['five:one']);                 // Undefined variable
unset($Array5['five:two'][1]);              // Undefined variable

$Array6 = array();
unset($Array6['six:one']);                  // no problem
unset($Array6['six:two'][1]);               // Undefined index

?>

Expected result:
----------------
No notices at all.

Considering no notice was produced on a non-existant scalar, I expected the same behavior when trying to unset various arrays, regardless of where the unset() all is made.

Actual result:
--------------
PHP 4.3.3
Notice:  Undefined property:  Array1 in t.php on line 12
Notice:  Undefined property:  Array1 in t.php on line 13
Notice:  Undefined index:  two:two in t.php on line 16
Notice:  Undefined variable:  Array3 in t.php on line 20
Notice:  Undefined variable:  Array3 in t.php on line 21
Notice:  Undefined index:  four:two in t.php on line 25
Notice:  Undefined variable:  Array5 in t.php on line 33
Notice:  Undefined variable:  Array5 in t.php on line 34
Notice:  Undefined index:  six:two in t.php on line 38

PHP 500b2-dev 200310010230
Notice:  Undefined index:  two:two in t.php on line 16
Notice:  Undefined variable:  Array3 in t.php on line 20
Notice:  Undefined variable:  Array3 in t.php on line 21
Notice:  Undefined index:  four:two in t.php on line 25
Notice:  Undefined variable:  Array5 in t.php on line 33
Notice:  Undefined variable:  Array5 in t.php on line 34
Notice:  Undefined index:  six:two in t.php on line 38


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-18 23:40 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Windows 2000 +Operating System: * -PHP Version: Irrelevant +PHP Version: *
 [2013-10-27 15:22 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2013-10-27 15:22 UTC] krakjoe@php.net
In every case, the notices are emitted because of a programming error:

unset($array5[1])

should first isset($array5) before un-setting a member, if you want to avoid undefined variable notices.

The same misconception creates all the notices you speak of.

Closing the bug.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 13:01:28 2024 UTC