4

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

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • see this question's answers for more explanation about isset and empty: http://stackoverflow.com/questions/1219542/in-where-shall-i-use-isset-and-empty – nickf Aug 14 '09 at 02:34
  • 1
    You don't have to add `isset`. `empty` will evaluate to true if the variable has not been set. – jason Aug 14 '09 at 02:35
  • But won't empty still allow php's notices to show if the variable is not set or not? – JasonDavis Aug 14 '09 at 02:41
  • 1
    Possible duplicate of [Why check both isset() and !empty()](http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty) – Oleg Dec 26 '15 at 12:07

2 Answers2

11

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.

Nick Presta
  • 28,134
  • 6
  • 57
  • 76
  • 14
    that's quite redundant since `empty` checks if a value is set – nickf Aug 14 '09 at 02:32
  • I know, but I'm answering the question with the given information. Such a thing is dangerous anyways since it's possible to be set and not empty but still contain malicious code or unexpected data. I'll update my answer to include this. – Nick Presta Aug 14 '09 at 02:35
  • 2
    @nickf That's not true. There are times when isset and empty can and should be used together. It's true that empty returns true if a variable is not set, BUT not every variable that isset is empty. Using isset and !empty is a good way to determine that a variable is set AND contains a usable value. – Nilpo Feb 29 '12 at 04:14
  • 4
    @Nilpo I think you got it backwards. `! empty($var) and isset($var)` is redundant because there is no possible value (or lack thereof) for `$var` that would cause `! empty($var)` to return `true` while causing `isset($var)` to return `false` (and if `! empty($var)` is `false`, then the conditional short-circuits, and `isset($var)` is not evaluated). The only way the two would not be redundant is if you wanted to check if `empty($var) and isset($var)`, which is the same as (but IMnsHO more confusing than) `empty($var) and ! is_null($var)`. –  Apr 22 '12 at 04:09
  • @Nilpo is right the following article does an excellent job of describing the subtle differences. http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/ – nickspiel Jul 16 '14 at 05:21
3

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;
nickf
  • 537,072
  • 198
  • 649
  • 721