identifying NULL or empty variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

identifying NULL or empty variable

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are functions such as is_null() and its siblings too.
Post Reply