18

I need to check if variables are set to something. Up till now I have been using strlen(), but that is really embarrassing as I am pretty sure that is not a very efficient function to be using over an over again.

How do I perform this sort of check more efficiently:

if (strlen($_GET['variable']) > 0)
{
    Do Something
}

Note that I don't want it to do anything if $_GET['variable'] = ''

Just to clarify what I mean - If I had www.example.com?variable=&somethingelse=1 I wouldn't want it to penetrate that if statement

Amy Neville
  • 10,067
  • 13
  • 58
  • 94

6 Answers6

34

You can try empty.

if (!empty($_GET['variable'])) {
  // Do something.
}

On the plus side, it will also check if the variable is set or not, i.e., there is no need to call isset seperately.

There is some confusion regarding not calling isset. From the documentation.

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

and...

That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

hw.
  • 790
  • 4
  • 10
  • 2
    @Winston, from the [documentation](http://in3.php.net/empty): A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist. – hw. Jun 08 '13 at 12:33
  • you're right! Sorry... But earlier, if you wrote like this, it might throw notice. – Winston Jun 08 '13 at 12:41
  • @Winston, I used to be confused by this too, but I have seen this in use since a long time without any notice (and I always keep `error_reporting` at its highest). Any idea which version might have made this change? – hw. Jun 08 '13 at 12:44
  • Yes what I saw in the php documentation was it said it was changing what empty() responds to between versions - hardly inspiring confidence – Amy Neville Jun 08 '13 at 12:51
  • @AmyNeville, where? I don't see this behaviour listed in the changelog in the documentation. The closest is change in `5.0.0` where "Objects with no properties are no longer considered empty." But I don't think this applies to your scenario. – hw. Jun 08 '13 at 12:59
  • Ok, I'm going to accept this answer. I think this is the correct one - thanks everyone and hw...:) – Amy Neville Jun 08 '13 at 13:12
  • `or if its value equals FALSE` - this might cause heavy problems, if you send 0 as parameter value. – marcinj Dec 21 '16 at 17:10
  • @marinj agreed, for me, i used isset($_GET['value']) because value could be zero which is acceptable in my case. – codecraig Apr 06 '17 at 12:01
8
 if(isset($_GET['variable']) && $_GET['variable']!=""){

}
Srikanth Kolli
  • 892
  • 1
  • 8
  • 19
  • 2
    what if an input will have `value = ""` isset will be true but still empty – Fabio Jun 08 '13 at 12:29
  • And yes, thats what the Q wanted: __Note that I don't want it to do anything if $_GET['variable'] = ''__ – Axel Amthor Jun 08 '13 at 12:30
  • @AxelAmthor, but the code above will do something if value = "". It is set, so the if condition will be `true`. – hw. Jun 08 '13 at 12:35
  • This seems like it would work now and be a bit more efficient than strlen probably hmmmm. So apparently there's no in-build function for this sadly :) – Amy Neville Jun 08 '13 at 12:39
  • @AmyNeville, actually, there is an inbuilt function: `empty`, as explained in my answer below. – hw. Jun 08 '13 at 12:47
  • isset - Determine if a variable is set and is not NULL. empty - Determine whether a variable is empty. empty() does not generate a warning if the variable does not exist. – Srikanth Kolli Jun 08 '13 at 12:55
4

If you just want to check if any $_GET is set, without knowing the value just count the $_GET array:

<?php
if (count($_GET) == 0):
    // do your stuff
else:
    // do your other stuff
endif;
?>
Calin Rusu
  • 49
  • 3
2
if(isset($_GET['variable']) && !empty($_GET['variable']))
{
//Do Something
}
Pank
  • 13,800
  • 10
  • 32
  • 45
  • 1
    You don't need to call `isset` if you are going to use `empty`. You can see my answer for details or check the [documentation](http://in3.php.net/empty). – hw. Jun 08 '13 at 12:46
0

You can use check for isset() but i would rather check also for not blank character with != ''

if (isset($_GET['variable'])) && ($_GET['variable']) != '')
Fabio
  • 23,183
  • 12
  • 55
  • 64
0

how about just

if ($_GET['variable'])
{
     Do Something
}
Steve Taylor
  • 301
  • 3
  • 14