24/7 Support: 800.608.6482

Get Started

Developer Docs

else Statement

If Else Statement

We've got the hang of if statements now. We check a condition, and if it's true, we execute some code. That works, and it's very handy, but what if we want to run some code if the condition is false? That's where the if-else statement comes in handy, it looks exactly like if statement, but before the statement closes, we can add , this gives us the option to run some different code if the first if statement is NOT true.

The code for an if-else statement looks like this:

The above code is the exact same as our original if statement, except now we have added some code to execute if the original condition is false. If we are on the Storefront page, we will see "Welcome to our awesome site!", but if we are on ANY other page besides SFNT, we will see a nice promotion for our sale on men's shirts.

elseif Statement

The statement can accomplish the same goal as writing two separate if statements, but why do that when we can simply combine it into one section? Using the elseif statement is simple, you open an if statement, check if a condition is true, then rather than exiting the if statement, we can check another condition with elseif.

The code for an elseif statement looks like this:

If you look at the code above, you'll notice that we are checking for the same condition as our original if statement, is the current Screen the SFNT. Immediately following that statement, rather than close the if statement, we put an elseif right after it. This time, we check if the Screen is PROD, if it is, we display a message about free shipping. If neither of those conditions are met, we exit the if statement and don't run any code at all. You can chain as many elseif together as your little heart desires.

Miva believes that all online businesses should have access to a scalable ecommerce platform that can meet their unique business requirements. Miva offers PCI compliant ecommerce, hosting, and custom website design and development solutions. Miva customers have processed over $100 billion in online sales since 1997.

Copyright © 2016 Miva, Inc - All Rights Reserved   Privacy Policy | Store Policy

Links
Contact Us
Receive Tips & Updates

Copyright © 2017 Miva, Inc - All Rights Reserved

Back To The Top