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
https://forums.phpfreaks.com/topic/251450-if-elseif-else-not-working/
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;
}

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.