Variables in AMPscript

AMPscript variable names always begin with the at sign (@).

Declaring Variables 

You can declare a variable using the Var keyword. When you declare a keyword, it is added to a dictionary of variables. The value of a newly declared variable is null. You can only declare variables in AMPscript blocks. In other words, you can’t declare variables in inline AMPscript strings.

This example declares a variable called “firstName”.

1%%[
2  Var @firstName
3]%%

You can use the Var keyword to declare multiple values in a single line. This example declares the “firstName”, “lastName”, and “contactCity” variables.

1%%[
2  Var @firstName, @lastName, @contactCity
3]%%

Setting Variables 

Use the Set keyword to assign a new value to a variable. This example declares the variable “contactCity” and sets its value to “Tokyo”.

1%%[
2  Var @contactCity
3  Set @contactCity = "Tokyo"
4]%%

You can set the value of a variable to the value of a function. This example sets the “currentDate” variable to the current date using the FormatDate() and Now() functions.

1%%[
2  Var @currentDate
3  Set @currentDate = FormatDate(Now(), "d MMMM yyyy")
4]%%

If the current date is August 5, 2023, the “currentDate” variable would have the value “5 August 2023”.