Using Variables

Variables aren’t required in AMPscript. It’s possible to achieve the same result by just using functions, conditional statements and operators. However, without variables:

  • functions and constants would need to be defined multiple times, often resulting in more code and making it harder to maintain
  • each duplicated AMPscript function needs to be interpreted again, resulting in potential performance issues when sending emails or rendering content.

The example below indicates the inclusion of two variables in AMPscript to retrieve a points balance and display a corresponding status level.

%%[

var @pointsBalance, @status
set @pointsBalance = Lookup("Points Balance","Balance","MemberId", _subscriberKey)

if @pointsBalance < 100 then
   set @status = "bronze"
elseif @pointsBalance > 100 and @pointsBalance < 500
   set @status = "silver"
else
   set @status = "gold"
endif

]%%

You are a valued %%=v(@status)=%% member.

When writing AMPscript, it’s good practice to optimize your code through setting variables for commonly used functions.