snorky Posted June 24, 2009 Share Posted June 24, 2009 I'm not getting the results I expect. It must be the way I'm defining the cases: switch ($emdiff) { case "": // emdiff is blank ( ... do some stuff ... ) break; case "!=''" and "NOT NULL": // emdiff is NOT blank and IS NOT NULL ( ... do some different stuff ... ) break; case "NULL": // emdiff IS NULL ( ... do nothing ... ) break; } In a switch statement, how do I deal with an empty field a non-empty field whose value is NULL a non-empty field whose value is NOT NULL Link to comment https://forums.phpfreaks.com/topic/163570-switch-statement/ Share on other sites More sharing options...
Andy-H Posted June 24, 2009 Share Posted June 24, 2009 Only run the statement if the variable is set. if ( isSet($var) ) { switch($var) { case "blah": //do this break; case "blahblah": //do this instead break; default: //do this if no case is true } } else { $err = 'Blah blah...'; } Link to comment https://forums.phpfreaks.com/topic/163570-switch-statement/#findComment-862991 Share on other sites More sharing options...
snorky Posted June 24, 2009 Author Share Posted June 24, 2009 I'm getting the variables from the database: while($row=mysql_fetch_array($rpt1)) { $id= $row["id"]; $lname= $row["lname"]; $fname= $row["fname"]; $emdiff= $row["emdiff"]; } Within the context of what you said, are those variables "set"? I've never used isSet(), and I've never had problems with switch statements before. But I've never used NULL and NOT NULL before, either. Back to the original question, In a switch statement, how do I form the cases to account for * an empty field * a non-empty field whose value is NULL * a non-empty field whose value is NOT NULL For an empty field, case "": usually works OK, but the exact syntax for NULL and NOT NULL elude me. Link to comment https://forums.phpfreaks.com/topic/163570-switch-statement/#findComment-862998 Share on other sites More sharing options...
thebadbad Posted June 24, 2009 Share Posted June 24, 2009 Yep, your (understanding of the) syntax is wrong. You can actually rearrange the cases and add a default to achieve what you want: <?php switch ($emdiff) { case '': // emdiff is blank break; case "NULL": // emdiff IS NULL break; default: //emdiff is neither 'blank' nor "NULL" } ?> Or switch() on true. Link to comment https://forums.phpfreaks.com/topic/163570-switch-statement/#findComment-863011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.