doubledee Posted February 6, 2012 Share Posted February 6, 2012 I am trying to force the "default" behavior in the Switch statement below. Why does having a "0" trigger the first Switch statement and not the last one?? switch (0){ /* switch ($resultsCode){ */ // Insert Succeeded. case 'ACCOUNT_MEMBER_ACCT_CREATED': echo '<h1>Member Account Created</h1>'; echo '<p>Congratulations!</p> <p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p> <p>Please click on the link in that e-mail to activate your account.</p>'; break; // Insert Failed. case 'ACCOUNT_MEMBER_ACCT_FAILED': echo '<h1>Account Creation Failed</h1>'; echo '<p>You could not be registered due to a system error.</p>'; echo '<p>Please contact the System Administrator.</p>'; break; // Default Error. default: echo '<h1>Error</h1>'; echo '<p>Oops! A system error has occurred. Please try again.</p>'; echo '<ul class="button2"> <li> <a class="testButton" href="' . BASE_URL . 'members/create_account.php">Create Account</a> </li> </ul>'; break; } Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/ Share on other sites More sharing options...
litebearer Posted February 6, 2012 Share Posted February 6, 2012 Just a stab... Since 0 is a number, php is converting your case string into a numeric value which happens to be zero. You are trying to compare a number to a string Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315189 Share on other sites More sharing options...
kicken Posted February 6, 2012 Share Posted February 6, 2012 Since 0 is a number, php is converting your case string into a numeric value which happens to be zero. You are trying to compare a number to a string Precisely. When comparing a string value and a numeric value, PHP opts to convert the string to a number. Since all your string values are invalid numbers they all equal 0, and php just goes to the first one in the list. If for example, you had done something like this (just an example to throw in a number): switch (0){ // Insert Succeeded. case '3D_ACCOUNT_MEMBER_ACCT_CREATED': echo '<h1>Member Account Created</h1>'; echo '<p>Congratulations!</p> <p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p> <p>Please click on the link in that e-mail to activate your account.</p>'; break; // Insert Failed. case 'ACCOUNT_MEMBER_ACCT_FAILED': echo '<h1>Account Creation Failed</h1>'; echo '<p>You could not be registered due to a system error.</p>'; echo '<p>Please contact the System Administrator.</p>'; break; } PHP would start at the second case because the first one would convert to the number 3, which does not equal zero. You can manually cast to a string to ensure PHP converts your code rather than the strings. eg: switch ((string)$resultsCode){ Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315216 Share on other sites More sharing options...
doubledee Posted February 6, 2012 Author Share Posted February 6, 2012 kicken, When I use this I get the last statement in my Switch... switch (99){ Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315250 Share on other sites More sharing options...
kicken Posted February 6, 2012 Share Posted February 6, 2012 When I use this I get the last statement in my Switch... Because 99 does not equal 0 (which is what the strings get converted to). Therefore none of you cases match so it goes to the default case. Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315267 Share on other sites More sharing options...
doubledee Posted February 7, 2012 Author Share Posted February 7, 2012 When I use this I get the last statement in my Switch... Because 99 does not equal 0 (which is what the strings get converted to). Therefore none of you cases match so it goes to the default case. All very informative! Thanks everyone, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315279 Share on other sites More sharing options...
doubledee Posted February 7, 2012 Author Share Posted February 7, 2012 kicken, So what is the safest way to call the "Default" option in a Switch statement? Right now I just use code like this... // Initialize variables. $resultsCode = (isset($_SESSION['resultsCode']) ? $_SESSION['resultsCode'] : 99); switch ($resultsCode){ Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315287 Share on other sites More sharing options...
kicken Posted February 7, 2012 Share Posted February 7, 2012 So what is the safest way to call the "Default" option in a Switch statement? Give you default a case label as well and use that. For example I do this on a lot of my pages: $action = isset($_GET['action'])?$_GET['action']:'list'; switch ($action){ case 'add': //... break; case 'edit': //... break; case 'delete': //... break; case 'list': default: //... break; } That makes it so that if the action is list, or any other un-recongized value it runs the default case. Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315288 Share on other sites More sharing options...
doubledee Posted February 7, 2012 Author Share Posted February 7, 2012 So what is the safest way to call the "Default" option in a Switch statement? Give you default a case label as well and use that. For example I do this on a lot of my pages: $action = isset($_GET['action'])?$_GET['action']:'list'; switch ($action){ case 'add': //... break; case 'edit': //... break; case 'delete': //... break; case 'list': default: //... break; } That makes it so that if the action is list, or any other un-recongized value it runs the default case. More correctly, it runs case 'list' which has no lines to execute AND that same line has NO EXIT so control drops down to the next line which is default:, right? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315290 Share on other sites More sharing options...
kicken Posted February 7, 2012 Share Posted February 7, 2012 More correctly, it runs case 'list' which has no lines to execute AND that same line has NO EXIT so control drops down to the next line which is default:, right? More or less. Each case marks a jump point. When PHP matches the value given to the switch to a given case value, it jumps to that location in the code and resumes execution from that point. It will keep executing from there until it encounters something that causes it to break out (end of switch, break statement, return-statemt). In the code above, both the 'list' case and the default case correspond to the same jump point so in either case it goes to the same place in the code. I could drop the "case 'list':" line all together and just have the default, but I prefer having the case line there to clearly mark it. Quote Link to comment https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/#findComment-1315325 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.