doubledee Posted March 24, 2012 Share Posted March 24, 2012 In a Switch statement, can you give the Default: a specific name, maybe like this... switch ($resultsCode){ // Missing Primary Key. case 'COMMENT_MISSING_KEYS_2050': echo '<h1>System Error</h1>'; echo '<p>A Fatal Error has occurred. Please contact the System Administrator. (2050)</p>'; break; default 'DEFAULT_CATCHALL_ERROR_CODE_9999': echo '<p>You have reached the catch-all error code... (9999)</p>'; break; Debbie Quote Link to comment Share on other sites More sharing options...
teng84 Posted March 24, 2012 Share Posted March 24, 2012 you dont have to put argument on your default its like else of if else statement Quote Link to comment Share on other sites More sharing options...
requinix Posted March 24, 2012 Share Posted March 24, 2012 You can have a case cascade into a default. case 'DEFAULT_CATCHALL_ERROR_CODE_9999': default: // code Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 24, 2012 Author Share Posted March 24, 2012 you dont have to put argument on your default its like else of if else statement But I'm writing the outcome of my Switch into my database, so I want a name/Label associated with "Default:". Debbie Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 24, 2012 Author Share Posted March 24, 2012 You can have a case cascade into a default. case 'DEFAULT_CATCHALL_ERROR_CODE_9999': default: // code Will that give me what I wanted in my OP? I'm not sure how that would work?! Can it blow up?! Debbie Quote Link to comment Share on other sites More sharing options...
trq Posted March 24, 2012 Share Posted March 24, 2012 But I'm writing the outcome of my Switch into my database, so I want a name/Label associated with "Default:". Can we see the code that does this? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 24, 2012 Share Posted March 24, 2012 from what i know you can not define a default case name however you can make a custom php function that runs your default code then do this...... default: //custom function to run default code default_code(); then define your default code in a function Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 24, 2012 Author Share Posted March 24, 2012 But I'm writing the outcome of my Switch into my database, so I want a name/Label associated with "Default:". Can we see the code that does this? Yes, but why?! Look, I have a page called "results.php" and I use it to display all non-form validation messages. (It is a clearinghouse of sorts.) Examples would include things like these codes... // Not Logged In. case 'PASSWORD_USER_NOT_LOGGED_IN_2105': // Set Redirect Path. $_SESSION['returnToPage'] = '/members/change_password.php'; echo '<h1>Not Logged In</h1>'; echo '<p>You must be logged in to change your password. (2105)</p>'; echo '<a class="button" href="' . BASE_URL . '/members/log_in.php">Log In</a>'; break; // Password Changed. case 'PASSWORD_NEW_PASSWORD_SET_2106': echo '<h1>Password Changed</h1>'; echo '<p>Your password has been changed.</p>'; echo '<p>Please log in using the new password. (2106)</p>'; echo '<a class="button" href="' . BASE_URL . '/members/log_in.php">Log In</a>'; break; In addition to displaying these when good and bad things occur, I am logging them into a "results" table along with other info to help me log errors, and so I'd like to know when "Default:" is firing as well. Follow me?! Debbie Quote Link to comment Share on other sites More sharing options...
requinix Posted March 24, 2012 Share Posted March 24, 2012 There's no logging in that code you've just posted. So where is it happening? Outside the switch? If you have it happen after then you can set some flag in the default case like $hit_default = true; and check for that when you log whatever. Quote Link to comment Share on other sites More sharing options...
doubledee Posted March 24, 2012 Author Share Posted March 24, 2012 There's no logging in that code you've just posted. So where is it happening? Outside the switch? If you have it happen after then you can set some flag in the default case like $hit_default = true; and check for that when you log whatever. I store my $resultsCode in the $_SESSION and at the top of the "results.php" file I run a block of code that takes that and things like User's ID, IP, Last Page On and write it to my database and then I echo the appropriate User Message based on the path in my Switch I hit. But again, why are you asking? I just wanted a more descriptive name than what Default gives. It appears the "cascading" suggestion above works and solves things... Debbie Quote Link to comment Share on other sites More sharing options...
trq Posted March 24, 2012 Share Posted March 24, 2012 But again, why are you asking? Because if we can see *how* your doing things we may be able to offer a solution. Quote Link to comment 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.