使用对不存在值的引用将变量设置为 NULL?

发布于 2024-10-06 08:27:27 字数 557 浏览 1 评论 0原文

当通过引用传递不存在的值时,PHP 会创建该值并将其设置为 NULL。当检查某些函数中的空值时内存增加时,我注意到了这一点。采用以下函数:

function v(&$v,$d=NULL){return isset($v)?$v:$d;}
$bar = v($foo, $default);

这将是:

if(isset($foo))
{
    $bar = $foo;
}
else
{
    $bar = $default;
}

但是,当传递不存在的变量时,PHP 会创建它们。对于变量 - 一旦方法/函数结束,它们就会被删除 - 但对于检查 $_GET 或 $_POST 等超级全局数组,数组元素永远不会被删除,从而导致额外的内存使用。

$request_with = v($_SERVER['HTTP_X_REQUESTED_WITH']);

任何人都可以解释为什么会发生这种情况,以及它是否是 PHP 待办事项修复或其他一些疯狂使用值的功能?

When passing a non-existent value by reference, PHP creates the value and sets it to NULL. I noticed it when memory increases were occurring while checking empty values in some functions. Take the following function:

function v(&$v,$d=NULL){return isset($v)?$v:$d;}
$bar = v($foo, $default);

This would be shorthand for:

if(isset($foo))
{
    $bar = $foo;
}
else
{
    $bar = $default;
}

However, when passing non-existent variables PHP creates them. In the case of variables - they are removed as soon as the method/function ends - but for checking super global arrays like $_GET or $_POST the array element is never removed causing extra memory usage.

$request_with = v($_SERVER['HTTP_X_REQUESTED_WITH']);

Can anyone explain why this happens and if it is a PHP todo fix or a feature for some other crazy use of values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2024-10-13 08:27:27

XeonCross 的函数 v 是经常使用的简写:

$val= isset($arr['elm']) ? $arr['elm'] : 'default'

避免可怕的“未定义索引:elm”通知。一个不错的辅助函数是:

function ifset(&$v1, $v2 = null) {
    return isset($v1) ? $v1 : $v2;
}

正如 Xeoncross 所建议的,所以你可以写得更好,

$val = ifset($arr['elm'],'default') 

但是,这在我们喜爱的“语言”(我们称之为 PHP)中有很多有趣的(?)怪癖:

在函数 ifset 中,$v1 似乎是 UNSET ,因此它正确返回值 $v2 并且您可能会得出结论 ifset 工作正常。但之后 $arr['elm'] 被默默地设置为 NULL。因此,请考虑以下事项:

    function wtf(&$v) {
       if (isset($v))
           echo "It is set";
       else
           echo "It is NOT set";
    }
    $p=[];
    wtf($p['notexist']);   => It is NOT set
    $p;                    => [ 'notexist' => NULL ]

但这是另一个错觉,因为 isset() 函数也对 NULL 值返回 false:

$x=NULL;
isset($x)   => false... huh??

我们是否期望这样?嗯..它在文档中,所以这也是设计使然。欢迎来到 php 的奇妙世界。

XeonCross' function v is a shorthand for the often used:

$val= isset($arr['elm']) ? $arr['elm'] : 'default'

to avoid the dreaded 'Undefined index: elm' notice. A nice helper function would be:

function ifset(&$v1, $v2 = null) {
    return isset($v1) ? $v1 : $v2;
}

as Xeoncross suggested, so you could write the much nicer

$val = ifset($arr['elm'],'default') 

however, this has a lot of interesting (?) quirks in our beloved "language" that we call PHP:

inside the function ifset, $v1 seems UNSET, so it correctly returns the value $v2 and you might conclude that ifset works ok. But afterwards $arr['elm'] is silently set to NULL. So consider the following:

    function wtf(&$v) {
       if (isset($v))
           echo "It is set";
       else
           echo "It is NOT set";
    }
    $p=[];
    wtf($p['notexist']);   => It is NOT set
    $p;                    => [ 'notexist' => NULL ]

But this is another delusion, as the isset() function returns false for NULL values as well:

$x=NULL;
isset($x)   => false... huh??

Did we expect this? well.. it is in the documentation, so this is by design as well. Welcome to the wonderful world of php.

没有心的人 2024-10-13 08:27:27

内存泄漏的原因是因为您告诉它。

当您请求参考参数时,PHP 会为您提供一个。当您使用未设置的变量调用函数时,PHP 将设置该变量,然后将引用传递给该新变量。当您使用超全局调用它时,它会创建丢失的索引。那是因为你告诉它的。

但是,我必须问为什么具体需要变量引用? 99.9% 的时间你并不真正需要它们。我怀疑这样做会很好:

function v($v, $d = null) { return isset($v) ? $v : $d; }

或者,如果您确实必须使用引用(您无法解决最初的问题),您还应该返回一个引用:

function &v(&$v, $d = null) { 
    if (isset($v)) {
        return $v;
    }
    return $d;
}

否则,获取引用而不是引用是没有意义的返回一个...

The reason you have the memory leak, is because you're telling it to.

When you ask for a reference parameter, PHP will provide you with one. When you are calling a function with an unset variable, PHP will set the variable and then pass the reference to that new variable. When you call it with a superglobal, it creates the missing index. That's because you told it to.

However, I must ask why specifically do you need variable references? 99.9% of the time you don't really need them. I suspect that it'll work just fine to do:

function v($v, $d = null) { return isset($v) ? $v : $d; }

Or, if you really must use references (which you can't get around your original problem with), you should also return a reference:

function &v(&$v, $d = null) { 
    if (isset($v)) {
        return $v;
    }
    return $d;
}

Otherwise it's pointless to take a reference and not return one...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文