Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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!

like image 806
Adam K Dean Avatar asked Apr 12 '12 19:04

Adam K Dean


People also ask

How do you fix a Undefined variable?

There are a few ways to fix an undefined variable: (1) You can rename the variable or variable set so that it matches what you have used in the topic; (2) you can re-insert the variable in the topic so that it uses an existing variable set/variable name, (3) you can add the undefined variable to the project as a new ...

How to stop Undefined variable error in PHP?

Fix Notice: Undefined Variable by using isset() Function This notice occurs when you use any variable in your PHP code, which is not set. Solutions: To fix this type of error, you can define the variable as global and use the isset() function to check if the variable is set or not.

Which is Undefined variable?

An undefined variable is a variable used in a program that was not previously declared in the source code. In most programming languages, this results in an error.


1 Answers

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.

like image 161
drew010 Avatar answered Sep 27 '22 22:09

drew010