figuringout Posted February 20, 2008 Share Posted February 20, 2008 I'm struggling to understand why this doesnt work. I have an array which I breakdown annd assign the various parts off to variables, then want to test if one is NULL if it is ignore it. $supplierEmail = $supplierarray[0]; $supplierId = $supplierarray[1]; $supplierEmail2 = $supplierarray[2]; in some cases $supplierEmail2 is null so I want to ignore it, so I thought I could do this. if ($supplierEmail2 == "NULL") echo "No email"; else echo $supplierEmail2 . "<br />"; this doesnt work and I cant figure out why. I even set the field to be something other than null and test it but still I dont get the results I thought I would get. Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/ Share on other sites More sharing options...
Chris92 Posted February 20, 2008 Share Posted February 20, 2008 use the empty() function to check if its NULL or false: if ( empty($supplierEmail2) ) { echo "No email"; } else { echo $supplierEmail2 . "<br />"; } Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/#findComment-471813 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 Try comparing it to strlen 0 instead. I dont think NULL is a string. Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/#findComment-471816 Share on other sites More sharing options...
schilly Posted February 20, 2008 Share Posted February 20, 2008 its NULL not "NULL" or do strcmp($string,"") == 0 Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/#findComment-471835 Share on other sites More sharing options...
figuringout Posted February 21, 2008 Author Share Posted February 21, 2008 I've tried everything you all suggested but still it does not work. What else could it be? The data is coming from a form. $supplier = $_POST[suppliers_info]; $supplierarray = explode(",",$supplier); $supplierEmail = $supplierarray[0]; $supplierId = $supplierarray[1]; $supplierEmail2 = $supplierarray[2]; then I test the variable $supplierEmail2 to see if is empty but it always returns true. I've even set the field in the database to be 'none' and tried to test for this, but still it doesnt work. Is it something to do with the variable coming from an array? Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/#findComment-472579 Share on other sites More sharing options...
uniflare Posted February 21, 2008 Share Posted February 21, 2008 if your testing if an email has been sent do this: if(!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $supplierEmail2) ) { echo("Email Address incorrect format"); } hope this helps, Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/#findComment-472663 Share on other sites More sharing options...
figuringout Posted February 21, 2008 Author Share Posted February 21, 2008 Thanks Uniflare your snippet will be very useful. Link to comment https://forums.phpfreaks.com/topic/92130-simple-if-statement-not-working/#findComment-472760 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.