php函数参数error suppression,empty()isset()仿真 [英] Php function argument error suppression, empty() isset() emulation

查看:120
本文介绍了php函数参数error suppression,empty()isset()仿真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这个问题的答案是否定的,但是如果有一些PHP大师,那么可以以无效的方式写一个函数参数或不存在的变量可以传入,php不会错误,而不使用'@'



像空和isset做。你可以传入一个刚刚组成的变量,它不会出错。

  ex:

空($ someBogusVar); //没有错误

myHappyFunction($ someBogusVar); // Php warning / notice


解决方案

总结,答案是不,你不应该(见下面的注意事项)。



此线程中有许多人提到的解决方法,例如在条件中使用引用变量或isset()或empty(),并在PHP配置中抑制通知。除了明显的解决方法,使用@,你不想要。



总结一个有趣的评论讨论与 Gerry :如果检查函数中的变量的值并正确处理未定义或空的情况,则通过引用传递变量确实有效。只是不要使用引用传递作为关闭PHP的方式(这是我原来不应该指向的地方)。


I'm pretty sure the answer to this question is no, but in case there's some PHP guru

is it possible to write a function in a way where invalid arguments or non existent variables can be passed in and php will not error without the use of '@'

Much like empty and isset do. You can pass in a variable you just made up and it won't error.

ex:

empty($someBogusVar); // no error

myHappyFunction($someBogusVar); // Php warning / notice

解决方案

Summing up, the proper answer is no, you shouldn't (see caveat below).

There are workarounds already mentioned by many people in this thread, like using reference variables or isset() or empty() in conditions and suppressing notices in PHP configuration. That in addition to the obvious workaround, using @, which you don't want.

Summarizing an interesting comment discussion with Gerry: Passing the variable by reference is indeed valid if you check for the value of the variable inside the function and handle undefined or null cases properly. Just don't use reference passing as a way of shutting PHP up (this is where my original shouldn't points to).

这篇关于php函数参数error suppression,empty()isset()仿真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆