Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

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

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 error is raised, but send the same to your own function and you'll get a Notice: Undefined offset: etc.

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;
}

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.

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]));

Like so.

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

Is there a way to allow undefined variables to pass to my function without raising errors?

Not interested in taking the easy option ;)

Thanks for reading!

o

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...