Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

isset

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

like image 576
JasonDavis Avatar asked Dec 09 '22 19:12

JasonDavis


2 Answers

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.

like image 156
Nick Presta Avatar answered Dec 13 '22 23:12

Nick Presta


I'm not sure exactly what you're after here. isset will check if a value has been set and return true if it has. empty will check if a value hasn't been set OR if it equates to false (eg: 0, "", null) and return true if it does.

I can't see why you'd need to combine the two. To rewrite your example without empty, you'd do this:

$pagesize = isset($_GET['pagesize']) && $_GET['pagesize']
          ? $_GET['pagesize']
          : 20;
like image 45
nickf Avatar answered Dec 13 '22 23:12

nickf