php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78443 Trying to create own isset/notempty function creates keys by ref
Submitted: 2019-08-22 10:58 UTC Modified: 2019-08-22 14:59 UTC
From: 6562680 at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.2.21 OS: Win10
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: 6562680 at gmail dot com
New email:
PHP Version: OS:

 

 [2019-08-22 10:58 UTC] 6562680 at gmail dot com
Description:
------------
When you pass undefined key by reference, the key will be created and filled with null value.

So you need manually remove these key or choose not to write own isset functions.

As an attention, you cannot remove these key inside function you pass key, you should unset it manually at same level you calling own isset.

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

// declare
function notempty(&$test, &$result = null)
{
    if (! (is_null($test)
      || ('' === $test)
      || (is_float($test) && is_nan($test))
    )) {
      $result = $test;
    }

    return isset($result);
}

// run
$arr = [0];
if (notempty($arr[1], $result)) dosomething($result);
var_dump($arr); // wtf, [0,null]

Expected result:
----------------
Auto remove created keys or just really "check" keys before create it...
Not sure its possible in C


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-22 14:59 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-08-22 14:59 UTC] requinix@php.net
That's right. For your function to receive a reference, it must be a reference *to something*. So the value is created implicitly. The same happens with a built-in function that takes a reference, even if the function does not try to write a value to it. https://3v4l.org/37dc5

Note that isset() and empty() are language constructs, which allow them to act in ways regular functions cannot.

If you want different emptiness checks than what empty() provides, you can always use isset() before the check:
  if (isset($arr[1]) && notempty($arr[1]))
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 12:01:29 2024 UTC