I'm using this ampscript block to set variables for my email content. Why does it work for all of the variables when there are only some declared in the top var? Is it best practice to declare all of them in the top var? What happens if I don't?


var @Source, @Medium, @Campaign, @Content, @Term, @LinkExtension

SET @Source = "csqns_email1"

SET @Medium = "marketing_email"

SET @Campaign = "csqns_journey"

SET @Content = AttributeValue("ProductCode")

SET @Term = AttributeValue("AddDate")


SET @FirstName = AttributeValue("FirstName")

SET @ProductName = AttributeValue("ProductName")

SET @QuoteAmount = AttributeValue("QuoteAmount")

SET @CoverageAmount = AttributeValue("CoverageAmount")

SET @Gender = AttributeValue("Gender")

SET @Age = AttributeValue("Age")

SET @ProductCode = AttributeValue("ProductCode")


2 Answers 2


Its a hit and miss. It is best practice to declare your variables because it throws an error sometimes when you dont.

If you have to debug alot of ampscript code, you declaring them lets you know that you have at least covered that base (i.e the error lies somewhere else)

  • 1
    I have yet to receive an error from not declaring variables... sometimes I think it's on old StackExchange legend, passed down from generation to generation. If I was a braver man I would say to forgo it completely, but here I am declaring variables and warning others to beware of the mystical validation beast. Oct 6, 2017 at 17:06
  • Lol!!! @AnonWonderer
    – 0xsegfault
    Oct 6, 2017 at 17:08
  • I have run into errors before. Declare your variables :)
    – 0xsegfault
    Oct 6, 2017 at 17:08

In the language elements documentation, the following is said:

Declaring a variable with the VAR keyword adds an entry to the Variables Dictionary with the variable name as the key and NULL as the value. If an entry for that name already exists, the value of the variable sets to NULL. If you use the variable to control a FOR loop, an attempt to declare it will result in a validation or runtime error.

So, generally speaking, you should declare everything except iterator variables. If you don't, unexpected behavior may occur if you try to read the value before setting it. Using var is recommended to ensure that all variables exist and are initialized to some value (NULL).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .