Rottingham Posted March 22, 2007 Share Posted March 22, 2007 Hello all, I have found over the last year or so, everytime I try to used predefined values for the switch/case checking, that it fails to recognize the defined value. I.e. define("VIEW_GENERAL", "0"); define("VIEW_ADDRESS", "1"); define("VIEW_FAMILY", "2"); function view_person($id, $category) { $person = $id; switch($category) { case VIEW_GENERAL: echo "View General Information"; break; case VIEW_ADDRESS: ... break; default break; } } This never works! Rather than using VIEW_GENERAL I need to hard code the switch cases with "0" or "1", etc etc. Is there any fix for this? Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/ Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 ran this <?php define("VIEW_GENERAL", "0"); define("VIEW_ADDRESS", "1"); define("VIEW_FAMILY", "2"); function view_person($category) { switch($category) { case VIEW_GENERAL: echo "View General Information"; break; case VIEW_ADDRESS: echo "View Address Information"; break; case VIEW_FAMILY: echo "View Family Information"; break; default: echo "View Default Information"; break; } } $a = array (0,1,2,3,0); foreach ($a as $cat) { echo "$cat : "; view_person($cat); echo '<br/>'; } ?> got this [pre] 0 : View General Information 1 : View Address Information 2 : View Family Information 3 : View Default Information 0 : View General Information [/pre] Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213028 Share on other sites More sharing options...
rtconner Posted March 22, 2007 Share Posted March 22, 2007 Nope, defined values work fine in switch statements. You are doing something else wrong. Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213030 Share on other sites More sharing options...
Rottingham Posted March 22, 2007 Author Share Posted March 22, 2007 (NOTE: Some of my code is innefficient, such as if/else statements. That is in the process of being cleaned, but everything is valid and working. Until I use defined values.) My Code // [ VIEW CATEGORIES ] define("VIEW_GENERAL", "0"); define("VIEW_ADDRESS", "1"); define("VIEW_FAMILY", "2"); define("VIEW_HISTORY", "3"); define("VIEW_CONTRIBUTIONS", "4"); define("VIEW_QUICKPRINT", "5"); function View_Person() { global $myTemp; global $Content; // If a specific category is not set, make it 0. if(!isset($_REQUEST["cat"])) $cat = 0; else $cat = $_REQUEST["cat"]; // If a view ID is not given, check for last_person_viewed in the session if($_REQUEST["view"] <= 0) if(isset($_SESSION["last_person_viewed"])) $ID = $_SESSION["last_person_viewed"]; else { GoTo("people.php"); exit; } else $ID = $_REQUEST["view"]; // Category switch($cat) { case VIEW_GENERAL: $view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_general"); show_general($view_person, $ID); break; case VIEW_ADDRESS: $view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_address"); show_address($view_person, $ID); break; case VIEW_FAMILY: GoTo("family.php?view=$ID"); exit; break; case VIEW_HISTORY: $view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_notes_history"); show_notes_history($view_person, $ID); break; case VIEW_CONTRIBUTIONS: break; default: echo "Default"; break; } $view_person = str_replace("{\$theme}", "people", $view_person); $Content .= $view_person; app_finish(); } my link: http://churchorg.macaction.org/people.php?view&cat=1 I can change cat to anything, and I always get default! As soon as I change the case to "0" or "1" or whatever the case may be, it works... that is why I am stumped. Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213034 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 Try putting something like echo "<h1>$cat</h1>"; immediately before the switch statement to check the value in $cat Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213036 Share on other sites More sharing options...
Rottingham Posted March 22, 2007 Author Share Posted March 22, 2007 I am doing that right now. The number is output, and then the Default echo from the default statement. such as, 0Default or 1Default echo "Category: $cat<br />"; // Category switch($cat) { case VIEW_GENERAL: $view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_general"); show_general($view_person, $ID); break; case VIEW_ADDRESS: $view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_address"); show_address($view_person, $ID); break; case VIEW_FAMILY: GoTo("family.php?view=$ID"); exit; break; case VIEW_HISTORY: $view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_notes_history"); show_notes_history($view_person, $ID); break; case VIEW_CONTRIBUTIONS: break; default: echo "Default"; break; } That gets me Category: 1 Default If I start a test.php and put this in, then it works fine... <?php define("ONE", "1"); define("TWO", "1"); define("THREE", "1"); $test = 1; switch($test) { case ONE: echo "Found a value of ONE"; break; case TWO: echo "Found a value of TWO"; break; case THREE: echo "Found a value of THREE"; break; default: echo "Default value!"; break; } ?> Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213038 Share on other sites More sharing options...
Rottingham Posted March 22, 2007 Author Share Posted March 22, 2007 Found the error... I moved all of the define statements to the beginning of the PHP file and that worked... Unsure why that would be, but apparently having them set half way through the php file, something took place to block the defines from working. Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213056 Share on other sites More sharing options...
Barand Posted March 22, 2007 Share Posted March 22, 2007 Were you calling the function before defining the constants? Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213058 Share on other sites More sharing options...
per1os Posted March 22, 2007 Share Posted March 22, 2007 You have to define constants first, general rule for any language. If you define them after the call to them it does not work. They do not work like functions do. Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213072 Share on other sites More sharing options...
Rottingham Posted March 22, 2007 Author Share Posted March 22, 2007 No, I had the defines before the function declaration, but the call to the function was made from another function placed earlier in the file. I suppose that would mea that at run time, the actual call was placed in memory before the define. I see the error of my ways! Thanks for the troubleshooting help. Link to comment https://forums.phpfreaks.com/topic/43886-switch-case-does-not-work-with-define-values/#findComment-213116 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.