Error reporting workaround?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Error reporting workaround?

Post 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?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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 {
    #...
}
???
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

That's cool. I do hope that gets implemented.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you do not pass the key, PHP will throw errors when it doesn't exist.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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!
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
  }
}
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

I prefer

Code: Select all

$action = @(int)$_GET['action'];
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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? ;)
Post Reply