garcon Posted March 25, 2008 Share Posted March 25, 2008 $facility_id = $_POST["facility_id"]; if($facility_id == NULL) echo "new"; else echo $facility_id; Why does the above code never echo new? Tis confusing me. I can post code more in full if needed. I've also tried if(!$facility_id) Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/ Share on other sites More sharing options...
rsammy Posted March 25, 2008 Share Posted March 25, 2008 try this: if($facility_id == '') echo "new"; Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/#findComment-500581 Share on other sites More sharing options...
dmccabe Posted March 25, 2008 Share Posted March 25, 2008 Can you do it this way too: if (!$facility_id) { echo "new"; } ? Nto saying you, but rather asking if that is the same thing? Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/#findComment-500585 Share on other sites More sharing options...
rsammy Posted March 25, 2008 Share Posted March 25, 2008 if $facility_id is NULL, it may sometimes contain a blank space. if this is the case, then if (!$facility_id) { echo "new"; } may not work as blank space is still considered a value. i may be wrong, but i've faced this before. i therefore play it safe by comparing with a blank space Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/#findComment-500590 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 You can use empty() or !$varname to check if it's NULL, empty, or not defined. If you want to check for null specifically, try $varname === NULL; <?php $var = NULL; if ($var === NULL) echo 'works'; ?> echo'ed 'works' for me. Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/#findComment-500591 Share on other sites More sharing options...
discomatt Posted March 25, 2008 Share Posted March 25, 2008 Also, try this: echo '(' . $_POST["facility_id"] . ')'; and copy the results in your source to here. It should return '()' if it is NULL, empty, or undefined. Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/#findComment-500595 Share on other sites More sharing options...
garcon Posted March 25, 2008 Author Share Posted March 25, 2008 I've solved it by making sure that $facility_id won't ever be NULL and instead is 0 now. Cheers for the help Link to comment https://forums.phpfreaks.com/topic/97839-how-to-check-a-variable-is-null/#findComment-500603 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.