Jump to content

if ... elseif ... else - not working!


Hyperjase

Recommended Posts

Hiya,

 

Trying to get this working based on a drop down posted from the step before.  Three options are dropoff, collect and ship.  This code always defaults to the first option, despite the fact that echoing the session shows the correct choice has been passed through.

 

Here's what I have:

 

if ($_SESSION['collection'] = "dropoff") {
	echo "Please drop it off with us";
} elseif ($_SESSION['collection'] = "collect") {
	echo "Please collect it from me";
} else {
	echo "Please ship it to you";
}

 

Any ideas what I'm missing here?

 

Thanks!

Link to comment
Share on other sites

And, I'll throw this out there . . . even though you only have three options right now, using a series of if . .elseif . . else statements can get messy. This is exactly what the switch() operator is for. This may look to be more work because of the additinoal lines of code, but it will be much more flexible and less error prone.

 

switch($_SESSION['collection'])
{
    case 'dropoff':
        echo "Please drop it off with us";
        break;
    case 'collect':
        echo "Please collect it from me";
        break;
    case 'ship':
    default:
        echo "Please ship it to you";
        break;
}

Link to comment
Share on other sites

I tested out after I posted my reply, it does work, I think I should word it better - it's nested within a case and switch already, so would be a case within a switch which is inside a primary switch and case.  Hopefully that makes it sound easier! Sorry for the confuddlement!

 

Jason

Link to comment
Share on other sites

I tested out after I posted my reply, it does work, I think I should word it better - it's nested within a case and switch already, so would be a case within a switch which is inside a primary switch and case.  Hopefully that makes it sound easier! Sorry for the confuddlement!

 

Jason

 

yes, you can have a switch() inside a switch() just like you can have a while() inside a while(), an if() inside an if(), etc.

Link to comment
Share on other sites

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.