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
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.