Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I pass undefined vars to functions without E_NOTICE errors?
    text
    copied!<p>Pretty simple question really, how do I pass undefined vars to functions without E_NOTICE errors?</p> <p>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.</p> <p>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 <em>any</em> of the args are set, rather than all like isset(a,b,c) does.</p> <pre><code>function anyset() { $argc = func_num_args(); $argv = func_get_args(); for ($i = 0; $i &lt; $argc; $i++) if (isset($argv[$i])) return true; else return false; } </code></pre> <p>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.</p> <p>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.</p> <pre><code>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])); </code></pre> <p>Like so. </p> <p>I could just do isset(x) || isset(x) || isset(x) but that doesn't look very nice.</p> <p><strong>Is there a way to allow undefined variables to pass to my function without raising errors?</strong></p> <p>Not interested in taking the easy option ;)</p> <p>Thanks for reading!</p> <p>\o</p> <p><strong>Update:</strong> 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:</p> <pre><code>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])); </code></pre> <p>Hope this helps someone else in the future!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload