IF Function

The IF function allows you to perform conditional processing (not to be confused with the IIF function which is a simplified version).

The IF function can be written with the following statements:

  • IF – (required) – Begin the IF conditionals with this statement
  • ELSEIF – (optional) – You can use the ELSEIF statement for conditional “else if” processing. Multiple ELSEIF statements can be used.
  • ELSE – (optional, can only be used once) – If all other statements are not satisfied, default to this else conditional
  • ENDIF – (required) – All IF conditionals need to end with the ENDIF statement.

Example 1:

Using just the ELSE conditional statement:



%%[
set @test = “abc”

IF @test == “abc” THEN
 set @response = “Hello, world!”
ELSE
 set @response = “Goodbye”
ENDIF
]%%
%%=v(@response)=%%

Output:

Hello, world!

Example 2:

Using ELSEIF statements:



%%[
set @test = “abc”

IF @test == “123” THEN
 set @response = “Hello, world!”
ELSEIF @test == “abc” THEN
 set @response = “Goodbye!”
ENDIF
]%%
%%=v(@response)=%%

Output:

Goodbye!

Explanation of Example 1:

Using IF conditional statements allows the statements to be evaluated depending on what is within the statements. Multiple ELSEIF statements can be used within the conditional statement. Must be opened with IF and closed with ENDIF.

In the first example, the variable @test is set as the string of “abc” and we’re using the IF function to evaluate the @test variable. Since @test does indeed equal to “abc” (since we set it in the line before) the IF function evaluates it as true and then sets the @response variable to equal “Hello, World!”

But if we used a different string in the first example, say, set @test = "notabc" then the IF statement would have evaluated @test == "abc" as false and therefore would have then defaulted to the ELSE statement resulting in the output of “Goodbye.” This is depicted in Example 1A below:

Example 1A:

Same code as Example 1 above but with a different string value for the @test variable.



%%[
set @test = “notabc”

IF @test == “abc” THEN
 set @response = “Hello, world!”
ELSE
 set @response = “Goodbye”
ENDIF
]%%
%%=v(@response)=%%

Output:

Goodbye

Explanation of Example 2:

Example 2 is similar to Example 1A above but using an ELSEIF statement. An ELSE statement can be also added to example 2 to have a default value return if none of the conditions hold true. Know that AMPscript evaluates from top to bottom so if any one of those conditions are met starting from the top, the IF statement will not evaluate anything below it. For example, let’s expand on Example 2 with more ELSEIF statements:

Example 2B:

Using ELSEIF statements and also adding an ELSE at the end before the closing ENDIF:



%%[
set @test = “abc”

IF @test == “123” THEN
 set @response = “Hello, world!”
ELSEIF @test == “abc” THEN
 set @response = “Goodbye!”
ELSEIF @test == “abc123” THEN
 set @response = “Is this thing on?”
ELSEIF @test == “aaaaaa” THEN
 set @response = “Anyone home?”
ELSE 
 set @response = “Nothing found”
ENDIF
]%%
%%=v(@response)=%%

Output:

Goodbye!

Note that the output is still “Goodbye!” and then rest of the ELSEIF statements are ignored since the condition has already been met since @test does equal to “abc”. The ELSE statement is also not evaluated because AMPscript reads from top to bottom and the condition has already been met before reaching the ELSE statement.

Example 3A (with AND operator):

Now suppose you want to use operators like AND and OR because you have more than one condition to evaluate. Instead of using multiple IF THEN statements over and over again, you can utilize these operators to include multiple conditions.

Let’s use an example that you want to both the size and color of a T-shirt:



%%[
set @size = “M”
set @color = “Purple”

IF @size == “M” AND @color == “Purple” THEN
 set @response = CONCAT(“Thank you for ordering your size “, @size, ” T-shirt in the color “, @color)
ELSE
 set @response = “Thank you for ordering your plain T-Shirt”
ENDIF
]%%
%%=v(@response)=%%

Output:

Thank you for ordering your size M T-shirt in the color Purple

Explanation of Example 3A (with AND operator):

Because the shirt size does equal to “M” AND also the color equals to purple, we are going to display the first response because using the AND operator means both conditions are true. We also used the Concat function to join our hard coded text with the two variables (size and color) to complete a sentence. See more on using the Concatenate function here.

Example 3B (with OR operators):

Let’s use a similar example but with the OR operator. Hint: With the OR operator, only one of the conditions need to be true.



%%[
set @size = “M”
set @color = “Green”

IF @size == “M” OR @color == “Purple” THEN
 set @response = CONCAT(“Thank you for ordering your size “, @size, ” T-shirt in the color “, @color)
ELSE
 set @response = “Thank you for ordering your plain T-Shirt”
ENDIF
]%%
%%=v(@response)=%%

Output:

Thank you for ordering your size M T-shirt in the color Green

Explanation of Example 3B (with OR operator):

Now we have set the color of the shirt “Green” and change the operator to an OR — the rest of the AMPscript stays the same.

The OR operator is evaluating if at least one of the conditions are true.

There are two conditions: either the shirt is a size “M” OR if the shirt is the color “purple.” The order of how this is written is important. AMPscript evaluates from top to bottom, left to right. If any of the first conditions are met in a function, it will stop evaluating the rest of that function.

The shirt size is a “M” so technically the AMPscript stops evaluating the rest of conditions since one of the condition is satisfied. The color of the shirt is green and the AMPscript is looking for purple, but since that is placed second in the IF statement and the first condition has been met, it does not evaluate that condition.

In an AND operator, it does not matter the order of how the conditions are written because ALL of the conditions need to be satisfied. In our previous example, it was both that the size needed to be “M” AND the color needed to be “Purple.”

Conclusion

I hope this makes sense and helps to clarify the IF THEN functions and also including AND and OR operators. These functions likely make up most of the AMPscript written because it can be super useful in evaluating your data in data extensions and displaying appropriate dynamic content based on that data. These are the building blocks needed to show dynamic content using data. Leave a comment below for any questions and I will be happy to help address anything. Thank you!

4 comments

  1. How about encrypting the data sent ? is there is a way to make the data sent not visible when checking the network in the console ?

  2. Do you know if a space is required after an IF statement? In other words, will “IF(…)” work, or does it need to be “IF (…)” ?

Leave a Reply

Your email address will not be published. Required fields are marked *