snorky Posted August 11, 2009 Share Posted August 11, 2009 PHP 4.3.8 I can't figure out the correct syntax for the case condition in a switch statement where one of the case conditions is a NULL value. :facewall: For a switch statement I have 3 conditions: [*]variable's value is NULL // NULL [*]variable's value is "" // blank & NOT NULL [*]variable's value is "abcdef" // not blank (and therefore NOT NULL) The switch statement looks like this switch(var) { case * (where * is some kind of NULL -- see below): do something #0; break; case "" do something #1; break; case "!=''" do something #2; break; } * Case NULL is the joker in the deck. I've tried it 5 different ways. case "= NULL": case NULL: case "NULL": case IS_NULL(): case "IS_NULL()": None of the 5 works correctly when the condition is a NULL value. Each of those has a different -- and incorrect -- result set for variable's value is NULL // NULL variable's value is "" // blank & NOT NULL variable's value is "abcdef" // not blank (and therefore NOT NULL) Link to comment https://forums.phpfreaks.com/topic/169803-php-switch-statement-and-null-values/ Share on other sites More sharing options...
wildteen88 Posted August 11, 2009 Share Posted August 11, 2009 You're using case wrong, but case null: should work. switch only compares the value of the variable you pass it. It does not allow you perform any conditions. $var = null; switch($var) { case null: echo '$var is NULL'; break; case '': echo '$var has no value or is emtpy'; break; } Link to comment https://forums.phpfreaks.com/topic/169803-php-switch-statement-and-null-values/#findComment-895800 Share on other sites More sharing options...
snorky Posted August 11, 2009 Author Share Posted August 11, 2009 You're using case wrong, but case null: should work. switch only compares the value of the variable you pass it. It does not allow you perform any conditions. $var = null; switch($var) { case null: echo '$var is NULL'; break; case '': echo '$var has no value or is emtpy'; break; } /* ---------------------- */ The goal is to produce a report. There is a while loop that spins out the report based on what happens in the while loop. The report looks fine (except that one column): all of the other elements of the report - including some generated by other switch statements (none of which have NULL as a possible value) - look as designed. (Note: I changed :one" to NONE" - oops!) Here's how I got the variable where NULL is a possible value: The table has a column named emdiff. The PHP to produce the report is next: while($row=mysql_fetch_array($rpt1)) .... $emdiff= $row["emdiff"]; // note: there several other columns in this mysql_fetch_array // all of them work as designed - including switch statements - but none of them has a condition where NULL is one of the possible values // in the process, values are assigned to $var2 and $var3 .... Then the switch statement in question: switch($emdiff) { case * (where * is some kind of NULL -- see below): print ""; break; case "" print $emdiff . " abcdef"; break; case "!=''" print $var2 . $var3; break; } // end switch statement } // end while loop In the result set a. when $emdiff is NULL the field displays as designed (meaning: it displays an empty result) b. when $emdiff is ="" print $emdiff . " abcdef"; does not display, even though it should display c. when $emdiff is !="" print $var2 . $var3; displays as designed When I use the other attempts at case * (some expression of NULL), I get various mixes of a, b, and c Link to comment https://forums.phpfreaks.com/topic/169803-php-switch-statement-and-null-values/#findComment-895835 Share on other sites More sharing options...
snorky Posted August 11, 2009 Author Share Posted August 11, 2009 In the above, The report looks fine (except that one column): all of the other elements of the report - including some generated by other switch statements (one of which has NULL as a possible value) - look as designed Should read The report looks fine (except that one column): all of the other elements of the report - including some generated by other switch statements (NONE of which have NULL as a possible value) - look as designed. That oopsie changes a lot.... sorry for the confusion. Link to comment https://forums.phpfreaks.com/topic/169803-php-switch-statement-and-null-values/#findComment-895838 Share on other sites More sharing options...
snorky Posted August 12, 2009 Author Share Posted August 12, 2009 I came up up with a way to get the desired result. It's kludgey, involving nested if-else statements. <?php // there are 3 possible values for $emdiff: // NULL, "" (blank), and (non-blank) if(isset($emdiff)) // if emdiff is NOT NULL { if($emdiff !='') // emdiff is NOT NULL and NOT blank { do task a; } else // emdiff is NOT NULL and is blank(empty) { do task b; } } else // otherwise, emdiff is NULL { do task c; } ?> If anyone can come up with a more elegant solution, I'd love to see it. I suspect that a switch/case would be better, but I struck out dozens of times trying. Link to comment https://forums.phpfreaks.com/topic/169803-php-switch-statement-and-null-values/#findComment-896832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.