Reputation: 7475
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!
Upvotes: 14
Views: 5366
Reputation: 1303
Pass by reference will work for defined arguments.
function my_isset(&$k) { ... }
$bool = my_isset($_POST['hi']);
Upvotes: 10
Reputation: 1011
Maybe something like this will work for you? It's a little bit hackish, but I think what you want is not quite possible without lazy evaluation of function arguments.
if (atLeastOneIsSet($myArray, 1, 3, 4, 6)) doSomething();
function atLeastOneIsSet(array $arr) {
$args = func_get_args();
for ($i = 1; $i < func_num_args(); ++$i)
if (isset($arr[$args[$i]])) return true;
return false;
}
Edit: oops, logic issues.
Upvotes: -1
Reputation: 69967
isset
is a bit special as it is actually a PHP language construct rather than a function [1].
If a variable is undefined, you can't really pass it because it isn't defined (hence the warning).
As stated in comments, you can suppress the warnings by prepending an @
to the variable which will prevent a warning from being emitted if it is not set. Then within the function you can check to see if the value is null or try using isset
to see if it is registered.
Upvotes: 5
Reputation: 33447
You have three options:
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.
Upvotes: 9