How do I pass undefined vars to functions without E_NOTICE errors?

前端 未结 4 1767
深忆病人
深忆病人 2020-12-20 12:48

Pretty simple question really, how do I pass undefined vars to functions without E_NOTICE errors?

When passing undefined variables to functions such as isset(), no e

4条回答
  •  旧时难觅i
    2020-12-20 12:59

    You have three options:

    • Use isset to handle the logic properly (isset($x) || isset($y) || isset($z))
    • Wrap everything in isset() and then have your function check if any of the arguements are true (kind of bleh)
    • Use @ to suppress errors (also bleh)

    An example of @ would be:

    function a($a) { } a(@$x);
    

    You should remember though that notices exist for a reason. I avoid error suppressing. It seems hacky to me. I would just properly wrap everything in isset(). It's a lot more verbose, but also, in my opinion anyway, more correct.

提交回复
热议问题