使用对不存在值的引用将变量设置为 NULL?
当通过引用传递不存在的值时,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XeonCross 的函数 v 是经常使用的简写:
避免可怕的“未定义索引:elm”通知。一个不错的辅助函数是:
正如 Xeoncross 所建议的,所以你可以写得更好,
但是,这在我们喜爱的“语言”(我们称之为 PHP)中有很多有趣的(?)怪癖:
在函数 ifset 中,$v1 似乎是 UNSET ,因此它正确返回值 $v2 并且您可能会得出结论 ifset 工作正常。但之后 $arr['elm'] 被默默地设置为 NULL。因此,请考虑以下事项:
但这是另一个错觉,因为 isset() 函数也对 NULL 值返回 false:
我们是否期望这样?嗯..它在文档中,所以这也是设计使然。欢迎来到 php 的奇妙世界。
XeonCross' function v is a shorthand for the often used:
to avoid the dreaded 'Undefined index: elm' notice. A nice helper function would be:
as Xeoncross suggested, so you could write the much nicer
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:
But this is another delusion, as the isset() function returns false for NULL values as well:
Did we expect this? well.. it is in the documentation, so this is by design as well. Welcome to the wonderful world of php.
内存泄漏的原因是因为您告诉它。
当您请求参考参数时,PHP 会为您提供一个。当您使用未设置的变量调用函数时,PHP 将设置该变量,然后将引用传递给该新变量。当您使用超全局调用它时,它会创建丢失的索引。那是因为你告诉它的。
但是,我必须问为什么具体需要变量引用? 99.9% 的时间你并不真正需要它们。我怀疑这样做会很好:
或者,如果您确实必须使用引用(您无法解决最初的问题),您还应该返回一个引用:
否则,获取引用而不是引用是没有意义的返回一个...
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:
Or, if you really must use references (which you can't get around your original problem with), you should also return a reference:
Otherwise it's pointless to take a reference and not return one...