3

i recently had to do a "test" for a job, and i got feed back saying that this statement was incorrect:

$images = $flickr->get_images(5, !empty($_GET['pg']) ? $_GET['pg'] : 1);

The "supposed" error was generated via the ternary operator on the first time the page was loaded, as there was no "?pg=1" (or whatever) passed via the query string.

The feed back said i should have used isset instead. I have looked at various posts both here (question 1960509) and blogs, but cannot find any definitive answer.

Is this really an error? How can i replicate this issue? do i need to put on E_STRICT or something in my php.ini file? Or might this be due to an older version of php?

Note: please don't tell me about how i should validate things.. i know this... it was a test to just see if i could use the flickr api calls.

Community
  • 1
  • 1
btwong
  • 49
  • 8
  • 1
    `empty()` implicitly calls `isset()` (or suppresses the error, I'm not sure which) - [Proof](http://codepad.org/jlTnQbFw). Your code is correct and is exactly what I would do. It certainly will not raise any errors. – DaveRandom Jul 18 '12 at 11:14
  • hmmm, I'd have gone with @is_int($_GET['pg']) – symcbean Jul 18 '12 at 11:59

6 Answers6

4

This is perfectly fine. empty is not an actual function, it's a language construct. It does not issue a warning if a variable is not set (in that case the variable is considered empty, thus the 'function' returns TRUE just as you want), and additionally it checks for empty or zero values.

You could see empty as a normal isset check with an additional loose comparison to FALSE:

empty($var) === (!isset($var) || $var == FALSE)
Joost
  • 10,333
  • 4
  • 55
  • 61
  • thanks Joost.From all that i read, it seemed to indicate that the `empty()` was perfectly fine to use here. I am meeting with my "tester" on friday, and i am going to see how he got this error. – btwong Jul 18 '12 at 11:21
0

$images = $flickr->get_images(5, (isset($_GET['pg']&&($_GET['pg']))) ? $_GET['pg'] : 1);

without isset you'll get error so combine them

Martin
  • 1,193
  • 3
  • 12
  • 24
0

I'd use

$images = $flickr->get_images(5, array_key_exists('pg', $_GET) ? $_GET['pg'] : 1);

Combine with !empty($_GET['pg']) if needed (i.e. array_key_exists('pg', $_GET) && !empty($_GET['pg'])), but array_key_exists is the intended function for this job.

TaZ
  • 743
  • 7
  • 15
0

The reason you would get an error is because the empty function is designed to check the value of an existing variable, whearas isset() is designed to tell you whether a variable has been instantiated, however because empty() is a language construct technically it doesn't throw an error or create a warning so most people don't see the difference.

From the docs:

empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set.
Joost
  • 10,333
  • 4
  • 55
  • 61
Dpolehonski
  • 948
  • 6
  • 11
0

I think in a situation like this isset is the correct function to use as it is checking the existence of the array element rather than checking if the value of the element has been set. As Martin notes, the best thing to do here is combine them as this will only check the value if the element exists, meaning that the error will not occur on the first page load.

Also, I think this will only give a warning if E_NOTICE is on (or perhaps E_WARNING as well)

hellsgate
  • 5,905
  • 5
  • 32
  • 47
  • It won't raise any kind of error or warning, `empty` is not a function, plus there's a great summary in Dave's comment and Joost's answer. – N.B. Jul 18 '12 at 11:15
  • @N.B.: I stand corrected. Apologies for giving out duff information here. – hellsgate Jul 18 '12 at 13:33
0

isset — Determine if a variable is set and is not NULL. So "isset" is the correct function to use for checking for value is set or not.

More details :http://php.net/manual/en/function.isset.php