Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/256553-unexpected-behavior-from-switch/
Share on other sites

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){

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

 

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

 

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.

 

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

 

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.

 

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.