Simple IF ELSE Statement in AMPScript

Feb 20, 2023 | AMPScript, Marketing Cloud

f-simple-if-else-statment-in-ampscript-kwamehq

To support my work, this post may contain affiliate links (these are referrer links sharing products and services. I get paid a small commission if you make a purchase through the link)

Simple IF ELSE Statement in AMPScript.

If else statements are the basic when writing logic in any programming language.

You can define an outcome when a condition is true or false.

Below is a simple example of an IF Else Statement using AMPScript to define the subject line for an email based on gender.

In our scenario, we have two gender values, M and F.

Simple IF ELSE Statement in AMPScript

The code snippet below defines two variables which will be used to store information. (@gender and @subjectline).

When the gender from the table equals M, the text “The boys have arrived.” will be stored in the @subjectline variable.

If the above case is false, the text “The girls are here.” is stored in the @subjectline variable.

Finally, there is an output function that writes the output value of the variable @subjectline

AMPScript Code


%%[
var @gender, @subjectline
set @gender = gender_flag /* field in DE */

if @gender == "M" then
    set @subjectline = “The boys have arrived.”
else
    set @subjectline = “The girls are here.”

output(v(@subjectline))

endif
]%%

Results

Gender_FlagOutput
MThe boys have arrived.
FThe girls are here.

Check out our other post on personalising first name using AMPScript

You can use multiple If else conditions in your code if required by using the “elseif” function.

AMPScript Code


%%[
var @gender, @subjectline
set @gender = gender_flag /* field in DE */

if @gender == "M" then
    set @subjectline = “The boys have arrived.”

elseif @gender == "Male" then
    set @subjectline = “The boys have here.”

else
    set @subjectline = “The girls are here.”

output(v(@subjectline))

endif
]%%

Results

Gender_FlagOutput
MThe boys have arrived.
MaleThe boys have here.
FThe girls are here.

You can learn more at the AMPScript Guide

0 Comments

Submit a Comment

Pin It on Pinterest