2

I have the following which doesn't work properly as $_GET['category'] can also equal 0.

if ( empty( $_GET['category'] ) ){
    // do something
} else {
    // do something else
}

How do I re-write this if statement to do 3 different things

  1. Do something if $_GET['category'] does not exist at all
  2. Do something if $_GET['category'] == 0
  3. Do something if $_GET['category'] == something other than "does not exist" and 0.

I've tried different combinations of empty(), !empty(), isset(), !isset() but I'm not getting the results I'm after. I'm probably doing the wrong combinations...

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
  • possible duplicate of [Why check both isset() and !empty()](http://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty) – KARASZI István Oct 04 '11 at 11:45
  • Your requirements seem to conflict: point 3 is the counterpart of point 1 and 2. Therefor point 1, 2 and 3 combined will match all cases. Did you mean point 3 should `do something else` by any chance? – Decent Dabbler Oct 04 '11 at 11:46
  • `===` also exists to. :) – Mob Oct 04 '11 at 11:47

6 Answers6

6

This isn't really hard to do: isset is the method you need.

if (!isset($_GET['category'])) {
    // category isn't set
} elseif ($_GET['category'] === '0') {
    // category is set to 0
} else {
    // category is set to something other than 0
}

Note that I have compared for exact equality to the string '0', because GET and POST variables are always strings (or occasionally arrays) and never numbers when PHP first receives them.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0
if (!isset($_GET['category']))
{
    1. Do something if $_GET['category'] does not exist at all
}
elseif ($_GET['category'] == 0)
{
    2. Do something if $_GET['category'] == 0
}
elseif (isset($_GET['category']) && ($_GET['category'] != 0)
{
    3. Do something if $_GET['category'] == something other than "does not exist" and 0.
}

My brackets might be slightly out somewhere but hopefully that should help you out.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
0

There is the filter_input-function for such cases.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0

Try this:

if (!isset($_GET['category'])) { // if variable not specified
}
elseif ($_GET['category'] == 0) { // variable is specified and zero
}
else { // variable is specified and not zero
}
Wug
  • 12,956
  • 4
  • 34
  • 54
Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71
0
<?php

$bad_values = array(null, '', 'dirty word');

if(isset($_GET['xd']))
{
    if(in_array($_GET['xd'], $bad_values, true))
    {
        // bad, it was null, empty or something else you think is not ok 
    }
    else
    {
        // it's fine, now check whether you got the zero. do a string comparison 

        if($_GET['xd'] == '0')
        {
            // you got the zero, yell at the user
        }
        else
        {
            // you got something else, it might be fine, it might be spoofed. use referential integrity to enforce data integrity in your db
        }
    }
}
else
{
    // it wasn't even sent in the request, that falls under 1)
}
N.B.
  • 13,688
  • 3
  • 45
  • 55
0

Make sure the name attribute of the element that is doing get on the previous page is set, preferably to the same as its id. Then:

$category='';
if ( !isset( $_GET['category'] ) ){
    //1
} else {
    $category=$_GET['category'];
    //3
  if($category==0){
    //2
   }
}
Paul Stanley
  • 4,018
  • 6
  • 35
  • 56