Page 1 of 1

identifying NULL or empty variable

Posted: Sat Apr 07, 2007 10:00 am
by sarris
Hi there. I am using AJAX/javascript to call a php script using POST method. I create the parameters string in javascript and pass it to the http_request.send() function.
In the php script i expect from 1 to 6 variables and that depends on the javascript. Moreover i am trying to identify in the script if the variables where passed or not.
I am using

Code: Select all

if(area2 != NULL) {$sql .= " OR $area2 = areas.id"; $FLAG=1;}
but doesnt seem to work. Even if area2 variable was not at all at the parameters string, it doesnt recognise it as NULL, therefor it goes on with what is inside the IF statement and then the sql querry crashes...Any ideas on how to identify if a variable is passed to the parameters string or not??
thanks

Posted: Sat Apr 07, 2007 10:07 am
by aaronhall
Strings in PHP are always preceded with a dollar sign ($area2), unless it's defined as a constant using define(). POST values should be accessed using the superglobal $_POST ($_POST['area2']). You can use empty() to check if the variable is null (unset), an empty string or an integer set to 0.

Posted: Sat Apr 07, 2007 10:09 am
by sarris
yes...i was just checking out that i didnt have the $ infront of area2. But empty() function will be quite more helpfull. thanks

Posted: Sat Apr 07, 2007 8:44 pm
by feyd
There are functions such as is_null() and its siblings too.