How can I use PHP's isset() in addition to empty()?

JasonDavis picture JasonDavis · Aug 14, 2009 · Viewed 11.4k times · Source

How would I add isset() and keep the empty() on my code below?

$pagesize = (!empty($_GET['pagesize'])) ? $_GET['pagesize'] : 20;

UPDATE:

I am just wanting to make sure php doesn't produce any notices or warnings

Answer

Nick Presta picture Nick Presta · Aug 14, 2009

Is this what you mean?

$pagesize = (isset($_GET['pagesize']) && !empty($_GET['pagesize'])) ? 
                $_GET['pagesize'] :
                20;

http://us.php.net/manual/en/language.operators.logical.php

EDIT:
To be complete, empty already checks if something is set, so you don't need to use isset() as well.
I would also caution against using this code if it is going directly into a query or something similar. Consider using intval, is_numeric and similar functions.