2

I prefer to develop with E_NOTICES turned on, but I often have to write excess, overly verbose, code when working with array indexes. How can I write this code in less code with E_NOTICES enabled.

With notices suppressed

if ($_REQUEST['some_key'] == 'bob')
{
}

Without notices suppressed

if (array_key_exists('some_key', $_REQUEST) && $_REQUEST['some_key'] == 'bob')
{
}
John Himmelman
  • 21,504
  • 22
  • 65
  • 80
  • *(sidenote)* "What's wrong with using $_REQUEST" - http://stackoverflow.com/questions/2142497/whats-wrong-with-using-request – Gordon Apr 23 '10 at 16:00
  • 1
    *(tip)* `error_reporting(-1)` will will show every possible error (including `E_STRICT`), even when new levels and constants are added in future PHP versions. – Gordon Apr 23 '10 at 16:03

2 Answers2

3

I generally use isset(), instead of array_key_exists(), for that kind of thing ; which means using something like this :

if (isset($_REQUEST['some_key']) && $_REQUEST['some_key'] == 'bob')
{
}


A couple of differences :

  • isset is a language construct, and not a function -- and its faster (no function call)
  • note that isset will return false if a data is null ; array_key_exists will return true
    • But it's often not a problem, especially when dealing with GET/POST parameters
  • it shorter to write (a couple less characters)
    • and you can use something like if (isset($_REQUEST['a'], $_REQUEST['b'], $_REQUEST['c']), if necessary
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I prefer array_key_exists() because isset() can't tell difference between NULL value and non-existant keys. – ZZ Coder Apr 23 '10 at 16:02
  • Thx Pascal for the robust answer. I think I'll stick to using an input/request library that allows for default values to be set on null/empty vars. ie, $w00lsworth = Input::get('the_var', 'default_value_if_not_passed'); – John Himmelman Apr 23 '10 at 16:13
  • @ZZ Coder: in what situations do you need to be able to tell the difference? I've never needed to do something with a variable that has been explicitly set to null. – DisgruntledGoat Apr 23 '10 at 16:15
  • 1
    @John : you're welcome :-) ;; of course, you are the one choosing the solution, in the end ;-) – Pascal MARTIN Apr 23 '10 at 16:23
  • 1
    @John using an abstraction for the Request is much better anyway, as it decouples you from the actual server environment. Can easily mock Requests this way. – Gordon Apr 23 '10 at 16:25
0

The easy way out is to use @ for error suppression:

if (@$_REQUEST['some_key'] == 'bob') {}

However, this can be very slow if you're using it more than once or twice per page load.

Another solution is to assign your unknown by reference (although I'm not 100% sure this will work for superglobals like $_REQUEST):

$some_key =& $_REQUEST['some_key'];
if ($some_key == 'bob') {}

Generally, I just use isset like Pascal said.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
  • 1
    I'd feel queasy looking at my code with @s splattered everywhere. It should be reserved for special use cases, and standout in your code (ie, if you're debugging a mysterious bug as a result of a suppressed message, it will be a pita, but at least it will be easy to find if it stands out in the code) – John Himmelman Apr 23 '10 at 16:16
  • 1
    Also, silencing the error is about 4 times slower than calling isset() – Kevin Schroeder Apr 23 '10 at 16:26