QuePID Posted May 30, 2011 Share Posted May 30, 2011 How can I check a variable to see if it is NULL and if so set to 0000-00-00? I've tried: if ($var IS NULL) THEN $var='0000-00-00'; to no avail. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/ Share on other sites More sharing options...
fugix Posted May 30, 2011 Share Posted May 30, 2011 if($var = null) ? Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222136 Share on other sites More sharing options...
QuePID Posted May 30, 2011 Author Share Posted May 30, 2011 if($var = null) ? It's not working for some reason here is the code: if ($date=NULL) $date="0000-00-00"; if ($col_datet=NULL) $col_datet="0000-00-00"; Here is the error given: Incorrect date value: '' for column 'Col_Datet' at row 1 What I am doing is retrieving a record from a mysql database in which the date and col_datet columns contain either a date or NULL. When I go to update the query with either of those two fields in which they contain a null the update query fails. So what I am wanting to do with PHP is to use a default date of 0000-00-00 instead of a NULL for the update query. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222139 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 NULL doesn;t catch empty strings (which your variables are. Empty string and NULL and NOT the same thing.) Try using empty() as in if (empty($date)) $date="0000-00-00"; Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222145 Share on other sites More sharing options...
QuePID Posted May 30, 2011 Author Share Posted May 30, 2011 NULL doesn;t catch empty strings (which your variables are. Empty string and NULL and NOT the same thing.) Try using empty() as in if (empty($date)) $date="0000-00-00"; Worked like a charm! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222147 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 if($var = null) ? No matter what the value of $var is, that will return TRUE. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222150 Share on other sites More sharing options...
fugix Posted May 30, 2011 Share Posted May 30, 2011 if($var = null) ? No matter what the value of $var is, that will return TRUE. stalker? Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222151 Share on other sites More sharing options...
Philip Posted May 30, 2011 Share Posted May 30, 2011 stalker? Only in my spare time, yes. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222170 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 if($var = null) ? No matter what the value of $var is, that will return TRUE. Correction: no matter what the value of $var is, that will return FALSE. *stalks* Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222205 Share on other sites More sharing options...
fugix Posted May 30, 2011 Share Posted May 30, 2011 if($var = null) ? No matter what the value of $var is, that will return TRUE. Correction: no matter what the value of $var is, that will return FALSE. *stalks* Lol. Salathe you're a funny guy. In my original post I was simply trying to correct his if statement. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222293 Share on other sites More sharing options...
salathe Posted May 30, 2011 Share Posted May 30, 2011 Lol. Salathe you're a funny guy. In my original post I was simply trying to correct his if statement. Trying, and failing. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222314 Share on other sites More sharing options...
Pikachu2000 Posted May 30, 2011 Share Posted May 30, 2011 if($var = null) ? No matter what the value of $var is, that will return TRUE. Correction: no matter what the value of $var is, that will return FALSE. *stalks* Yes, with a NULL you're correct. Point being, it needs a comparison operator, not an assignment operator, to work as intended. Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222339 Share on other sites More sharing options...
Zurev Posted May 30, 2011 Share Posted May 30, 2011 No one suggested the use of is_null? I mean empty works, but also if it's equal to 0. http://www.php.net/manual/en/function.is-null.php Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222343 Share on other sites More sharing options...
fugix Posted May 30, 2011 Share Posted May 30, 2011 Lol. Salathe you're a funny guy. In my original post I was simply trying to correct his if statement. Trying, and failing. :'( Quote Link to comment https://forums.phpfreaks.com/topic/237823-checking-variable-for-null/#findComment-1222541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.