Bug #78443 [Opn->Nab]: Trying to create own isset/notempty function creates keys by ref

From: Date: Thu, 22 Aug 2019 14:59:44 +0000
Subject: Bug #78443 [Opn->Nab]: Trying to create own isset/notempty function creates keys by ref
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222369@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78443&edit=1 ID: 78443 Updated by: requinix@php.net Reported by: 6562680 at gmail dot com Summary: Trying to create own isset/notempty function creates keys by ref -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Win10 PHP Version: 7.2.21 Block user comment: N Private report: N New Comment: 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])) Previous Comments: ------------------------------------------------------------------------ [2019-08-22 10:58:55] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78443&edit=1

« previous php.bugs (#222369) next »