如何将未定义的var传递给没有E_NOTICE错误的函数? [英] How do I pass undefined vars to functions without E_NOTICE errors?

查看:83
本文介绍了如何将未定义的var传递给没有E_NOTICE错误的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实是一个非常简单的问题,如何将未定义的var传递给没有E_NOTICE错误的函数?

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

将未定义的变量传递给isset()之类的函数时,不会引发任何错误,但是将其发送到您自己的函数中,您会得到一个通知:未定义的偏移量:等等.

When passing undefined variables to functions such as isset(), no error is raised, but send the same to your own function and you'll get a Notice: Undefined offset: etc.

我已经想到了今天要这样做的几个原因,但是我当前的功能几乎是isset的克隆,除了它将检查是否设置了 any 的args,而不是像isset( a,b,c).

I have thought of a few reasons to want this today, but my current function is almost a clone of isset except it will check if any of the args are set, rather than all like isset(a,b,c) does.

function anyset()
{
    $argc = func_num_args();
    $argv = func_get_args();
    for ($i = 0; $i < $argc; $i++) 
        if (isset($argv[$i])) return true;
    else return false;
}

现在,例如,我有一个[x] [y]的2d巨型数组,值将随机放置在其中.我需要检查随机坐标是否包含下一个"(x-1,y-1至x + 1,y + 1)等.

Now, I have for example a giant 2d array of [x][y], into which values will be placed at random. I need to check the randomized co-ords contains anything "next" to it (x-1,y-1 to x+1,y+1) etc.

我不想做一个20,000,000的循环并初始化每个变量.我只想发送9个变量,并检查是否已设置.

I do not want to do a loop of 20,000,000 and initialise each variable. I just want to send 9 vars and check if any are already set.

while (anyset($items[$x-1][$y-1],$items[$x][$y-1],$items[$x+1][$y-1],
              $items[$x-1][$y],$items[$x][$y],$items[$x+1][$y],
              $items[$x-1][$y+1],$items[$x][$y+1],$items[$x+1][$y+1]));

像这样.

我可以做isset(x)|| isset(x)|| isset(x),但是看起来不太好.

I could just do isset(x) || isset(x) || isset(x) but that doesn't look very nice.

有没有办法允许未定义的变量传递给我的函数而不会引发错误?

对采用简单选项不感兴趣;)

Not interested in taking the easy option ;)

感谢阅读!

\ o

更新:2012年4月12日,21:03 看起来似乎没有特殊功能允许发生这种情况.因此,要么像anyset(@ $ array [0],@ $ array [1])这样传递,要么将所有东西包装成一千个isset,像这样:

Update: 12 April 2012, 21:03 Looks like there is no special feature allowing this to happen. So either pass like anyset(@$array[0], @$array[1]) etc, or just wrap everything in a thousand issets like so:

while (isset($items[$x-1][$y-1]) || isset($items[$x][$y-1]) || isset($items[$x+1][$y-1]) || 
       isset($items[$x-1][$y])   || isset($items[$x][$y])   || isset($items[$x+1][$y]) || 
       isset($items[$x-1][$y+1]) || isset($items[$x][$y+1]) || isset($items[$x+1][$y+1]));

希望这对以后的人有帮助!

Hope this helps someone else in the future!

推荐答案

您有三个选择:

  • 使用isset正确处理逻辑(isset($ x)|| isset($ y)|| isset($ z))
  • 将所有内容包装在isset()中,然后让您的函数检查是否有任何论点是正确的(有点类似)
  • 使用@来抑制错误(也很容易)

@的示例为:

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

您应该记住,尽管存在某些原因是存在这些注意事项.我避免错误抑制.对我来说似乎很客气.我只是将所有内容正确包装在isset()中.冗长得多,但我认为还是更正确.

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.

这篇关于如何将未定义的var传递给没有E_NOTICE错误的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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