Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/259595-can-give-default-a-name/
Share on other sites

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

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

 

 

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.

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.