Page 1 of 1

Error reporting workaround?

Posted: Mon Mar 19, 2007 6:44 pm
by alex.barylski
Basically I have the following:

Code: Select all

$action = (int)$_GET['action'];
PHP error reporting at full pickiness complains when the GET isn't set, how do I stop this without error surpression?

Posted: Mon Mar 19, 2007 6:53 pm
by Skara

Code: Select all

$action = isset($_GET['action']) ? (int)$_GET['action'] : 0;
or

Code: Select all

if (isset($_GET['action'])) {
    $action = (int)$_GET['action'];
}
else {
    #...
}
???

Posted: Mon Mar 19, 2007 6:54 pm
by volka

Code: Select all

$action = isset($_GET['action']) ? (int)$_GET['action'] : 0;
There will probably a shorter expression in php6, see http://www.php.net/~derick/meeting-note ... thing-else

Posted: Mon Mar 19, 2007 6:55 pm
by Skara
That's cool. I do hope that gets implemented.

Posted: Mon Mar 19, 2007 6:55 pm
by alex.barylski
Thats basically what I've done, but I odn't want to type that out everytime, so I plopped it into a function:

Code: Select all

function _init_var($var, $key, $val)
  {
    return (!empty($var[$key]) && isset($var[$key]) ? $var[$key] : $val);
  }
Originally i asked if this function was already redundant, but it was slightly different and didn't work (basically still required $var to be set as $_GET['index] which obviously still spits an error...this function works fine, but is it redundant?

Posted: Mon Mar 19, 2007 7:07 pm
by Skara
Hm. But it should still throw an error if the $var variable isn't set when you call _init_var(), no?

In any case, I don't know why you're passing a key anyway when blahfunction($var['keyname'],'value'); will work.

Posted: Mon Mar 19, 2007 9:05 pm
by feyd
If you do not pass the key, PHP will throw errors when it doesn't exist.

Posted: Mon Mar 19, 2007 9:21 pm
by John Cartwright
volka wrote:

Code: Select all

$action = isset($_GET['action']) ? (int)$_GET['action'] : 0;
There will probably a shorter expression in php6, see http://www.php.net/~derick/meeting-note ... thing-else
woohoo!

Posted: Mon Mar 19, 2007 9:44 pm
by nickvd
Jcart wrote:
volka wrote:

Code: Select all

$action = isset($_GET['action']) ? (int)$_GET['action'] : 0;
There will probably a shorter expression in php6, see http://www.php.net/~derick/meeting-note ... thing-else
woohoo!
Ditto!!

I'm sure every single large app I've seen has it's own thisorthat() type function, it's about time to see the 'problem' 'fixed' in the core.

Posted: Tue Mar 20, 2007 3:10 am
by alex.barylski
feyd wrote:If you do not pass the key, PHP will throw errors when it doesn't exist.
You mean for my function or the original statement? My function assumes each will be set...and they always will be. :)

I just hate using error surpression and needed a simple function do do the trick as superglobal access is common - I use POST and GET inside individual controllers so I don't initialize these in the front controller (bad design or not).

I'm drunk...happy birthday to me... :P

I can't be bothered to write anymore...I need my beauty sleep :)

Posted: Tue Mar 20, 2007 8:13 am
by feyd
Hockey wrote:You mean for my function or the original statement? My function assumes each will be set...and they always will be. :)

I just hate using error surpression and needed a simple function do do the trick as superglobal access is common - I use POST and GET inside individual controllers so I don't initialize these in the front controller (bad design or not).
I was replying to Skara's statement that $foo['bar'] would work, instead of sending the key to the function. Your function appears okay.

Here's several utility functions I wrote some time ago

Code: Select all

function fetch($element, $default = '', $array = '') {
  if(func_num_args() < 3 or !is_array($array)) {
    $array =& $_REQUEST;
  }
  if(check($element, $array)) {
    $return = $array[$element];
  } else {
    $return = $default;
  }
  return $return;
}


function check($element, $array = '') {
  if(func_num_args() < 2 or !is_array($array)) {
    $array =& $_REQUEST;
  }
  $return = (!empty($array) and isset($array[$element]));
  return $return;
}


function makeDefine($name, $value = true) {
  if(!defined($name)) {
    define($name,$value);
  }
}

Posted: Tue Mar 20, 2007 9:12 am
by stereofrog
I prefer

Code: Select all

$action = @(int)$_GET['action'];

Posted: Tue Mar 20, 2007 2:33 pm
by Mordred
Duh, I have a set of functions that behave in the same manner (but better ;) ) as this ifsetor:

$nInt = GetInt($_REQUEST['int'], 5, 0, 10); //interpret the value as int and force it to be between 0 and 10. If not set, use 5 instead.
(0, 10 are of course optional)

Ditto for strings, floats, enums, and arrays thereof.

This can be done with a simple function in PHP4, why wait for PHP6 to implement a special operator for it? ;)