Skip to main content

Review AMPscript Basics

Learning Objectives

After completing this unit, you’ll be able to:

  • Review AMPscript language elements.
  • Identify AMPscript functions.
  • Use date and math functions.

Before You Begin

Before you begin, make sure you complete AMPscript for Marketers. While the badge is geared toward marketers with little coding experience, it serves as a great foundation for anyone learning AMPscript. The work you do here builds on the concepts and work you do in that badge. In this badge, we assume that you are a technical marketer with basic knowledge of HTML and CSS. 

AMPscript Review

AMPscript is Marketing Cloud Engagement’s proprietary scripting language that helps marketers enhance their messages. From adding personalized content to retrieving relational data from data extensions, AMPscript is a powerful friend to have, especially as a technical marketer. 

In AMPscript for Marketers, you learn about the language of AMPscript from characters, variables, and operators. We even show practical examples of how to use strings of code. In this badge, we take you to the next level, where we help you start using AMPscript in specific use cases. /* Are you ready for this? */ 

AMPscript Basics

Let’s get started by doing a quick review of AMPscript basics. The AMPscript language is procedural and processed top down. This means the system scans and outputs all AMPscript in the order it was written, before sending the email to subscribers. Therefore, the system needs to know where the AMPscript code starts and stops. To identify AMPscript, the code must be surrounded by opening and closing delimiters or defined within a script tag. It’s a flexible language with three options for defining the AMPscript code. 

Type
Sample Code

Inline

%%=LowerCase(FavoriteAnimal)=%%

Block

%%[Output(LowerCase(FavoriteAnimal))]%%

Tag 

<script runat=server language=ampscript> Output(LowerCase(FavoriteAnimal))</script>

Note

In this module, we focus on inline and block AMPscript, but you can find examples of script tags on the AMPscript developer site.

Values

One of the core language elements of AMPscript are values, which can be either variables, constants, or attributes. Let’s start with variables. 

Variables 

A variable is a storage location for the information you want to reference later within a script statement or function. Variable names must begin with @ and include at least one other letter, number, or underscore. For example: @firstName or @first_Name.

Note

Spaces and commas aren’t allowed in a variable name.

Variables are declared in your code by using the keyword VAR followed by one or more comma-delimited variable name(s). Once declared, variables are assigned using the keyword SET followed by the variable name, equal sign, and value. A variable’s value can be a constant or an attribute. 

Constants and Attributes

Value Type
Description
Code Example

Constant

A constant is a value that cannot be altered by the system. It is what it is when it is set. These can be numeric, string, Boolean, or null values. 

%%[ VAR @firstName
SET @firstName = "Susan"
]%%

Attribute

Attributes (or data extension values) refer to any content held inside a subscriber list or a data extension. They are recognized as unquoted strings. Values that have a space or special character require opening and closing brackets. Therefore it is best practice to include opening and closing brackets around all values for consistency and troubleshooting.

%%[ VAR @firstName
SET @firstName = [First Name]
]%%

Data Comparison

A key component of AMPscript is the ability to compare and add data into an email. AMPscript uses common operators for data comparison criteria, like greater than, equal to, and so on. However, they also use a few additional operators.

Operator
Used For

==

Is Equal To 

!=

Not Equal To 

AND, OR, NOT

Comparison modifiers

Comparison operators are controlled in AMPscript by using parentheses. So if you are looking for a new orange tabby cat or kitten, your comparison operators might be: (Cat AND Orange) OR (Kitten AND Orange).

IF Syntax

Now let’s review the IF syntax, which is used to define data or narrow down results based on set criteria. 

Operator
Additional Info

IF 

To perform conditional processing.

ELSEIF

Multiple ELSEIF statements can appear within an IF block.

ELSE

Only one ELSE statement can appear within an IF block.

ENDIF

Use the ENDIF statement to end an IF block. Only one ENDIF statement must appear at the end of an IF block.

Example Code

Let’s see an example of how the IF syntax would be used to set a default first name for a customer whose first name is missing in your data.

%%[ VAR @firstName
SET @firstName = [First Name]
IF Empty(@firstName) THEN
SET @firstName = "NTO Rockstar"
ENDIF
]%%
Note

AMPscript is not case sensitive, however, it is a good idea to pick a standard and be consistent in your code.

A Comment on Comments

Every good programmer knows how important it is to add comments to your code. Doing so makes code easier for yourself and others to understand the purpose of the code. But did you know that AMPscript comments are ignored by the application and do not show up in the final deployment? This differs from HTML comments. HTML comments can still be viewed after deployment by inspecting the source code of the sent message. /* nice comments only */ 

AMPscript Functions

The previous language elements are all used within functions. Functions are self-contained modules of code that accomplish specific tasks. Functions usually take in data, process it, and return a result. For example, the function %%=Format(@price, "C", 'en-US')=%% updates a product’s price to reflect the proper formatting for US currency.

Functions are written like this: FunctionName(X)

  • FunctionName: A unique name that describes the function.
  • ( ): Opening and closing parentheses.
  • X: The parameters being evaluated. If there are multiple, they are separated by commas, like 1, 2, 3.

Functions can accept variables, constants, and attributes as parameters between the parentheses. 

Note

In the examples found in the rest of the module, functions are shown as FunctionName(1, 2, 3), which represent specific parameters.

Common AMPscript Functions

While AMPscript doesn’t support the creation of custom functions, there are plenty of built-in functions that can be easily added to your code. Let’s review some date and time functions and sample code. 

Date Functions

Click on the name of each function to see how to use it and example code with output. You can also click on the link for each function to view developer documentation.

Working with date and time values can take some practice. Here are some things to keep in mind.

  • Dates show up in data in many ways; be sure to bookmark this developer page on AMPscript Date and Time Formatting.
  • Marketing Cloud Engagement’s system time is central standard time, and it does not adjust for daylight saving time.

Math Functions

By learning how to do math with AMPscript, you’ll be able to write complex math equations to calculate numbers on the fly using data from data extensions, as well as constant numbers that you may want to define. Let’s do some math. Click on the name of each function to see how to use it and example code with output.

Note
So many AMPscript functions to try! Here is a full list of functions and descriptions to spark your ideas.

Now that you have a handle on the basic aspects of AMPscript, in the next unit we review AMPscript string functions.

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities