{"id":31302,"date":"2018-04-21T15:47:44","date_gmt":"2018-04-21T15:47:44","guid":{"rendered":"https:\/\/ampscript.guide\/scope\/"},"modified":"2021-04-06T01:18:15","modified_gmt":"2021-04-06T01:18:15","slug":"scope","status":"publish","type":"post","link":"https:\/\/ampscript.guide\/scope\/","title":{"rendered":"Scope"},"content":{"rendered":"<h3>Scope<\/h3>\n<p>Once a variable is set, its scope is set globally; that is, it can be referenced later in the code to display the output, or used in conditional expressions. However, variables can also be changed after they have been set. For example, a promotion end date is set from a field in a Sendable Data Extension using a personalization string, but for specific customers, there is a requirement to adjust this value later in the code:<\/p>\n<pre><code>%%[\n\nvar @promotionEndDate, @memberStatus\nset @promotionEndDate = '10\/15\/2018'\nset @memberStatus = status\n\nif @memberStatus == 'gold' then\n   set @promotionEndDate = '11\/15\/2018'\nendif\n\n]%%<\/code><\/pre>\n<p>Variables can also be set on themselves in a function; that is, if the variable has previously been declared, then it can be used as an argument to a function, based on its previous value. So the above code could also be expressed as:<\/p>\n<pre><code>%%[\n\nvar @promotionEndDate, @memberStatus\nset @promotionEndDate = '10\/15\/2018'\nset @memberStatus = status\n\nif @memberStatus == 'gold' then\n   set @promotionEndDate = DateAdd(@promotionEndDate, 1, 'M')\nendif\n\n]%%<\/code><\/pre>\n<p>Also, once a variable has been set, it can be reset by declaring the value again. In the example below, if the current date is less than (after) a membership end date, then a <code>null<\/code> value will be set for membership status.<\/p>\n<pre><code>%%[\n\nvar @membershipEndDate, @memberStatus\nset @membershipEndDate = '10\/15\/2018'\nset @memberStatus = status\n\nif @membershipEndDate &gt; Now() then\n   var @memberStatus\nendif\n\n]%%<\/code><\/pre>\n<p>The only exception to this scope rule is when a variable is used within a <code>for<\/code> block. In this scenario, the variable is locked from modification within the <a href=\"\/process-loops\">process loop<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scope Once a variable is set, its scope is set globally; that is, it can be referenced later in the code to display the output, or used in conditional expressions. However, variables can also be changed after they have been set. For example, a promotion end date is set from a field in a Sendable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v14.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The AMPscript Guide - Scope<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ampscript.guide\/scope\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The AMPscript Guide - Scope\" \/>\n<meta property=\"og:description\" content=\"Scope Once a variable is set, its scope is set globally; that is, it can be referenced later in the code to display the output, or used in conditional expressions. However, variables can also be changed after they have been set. For example, a promotion end date is set from a field in a Sendable [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ampscript.guide\/scope\/\" \/>\n<meta property=\"og:site_name\" content=\"The AMPscript Guide\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-21T15:47:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-06T01:18:15+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ampscript.guide\/#website\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"The AMPscript Guide\",\"description\":\"The Definitive Scripting Manual for Salesforce Marketing Cloud\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/ampscript.guide\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/scope\/#webpage\",\"url\":\"https:\/\/ampscript.guide\/scope\/\",\"name\":\"The AMPscript Guide - Scope\",\"isPartOf\":{\"@id\":\"https:\/\/ampscript.guide\/#website\"},\"datePublished\":\"2018-04-21T15:47:44+00:00\",\"dateModified\":\"2021-04-06T01:18:15+00:00\",\"author\":{\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\"},\"breadcrumb\":{\"@id\":\"https:\/\/ampscript.guide\/scope\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ampscript.guide\/scope\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ampscript.guide\/scope\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/scope\/\",\"url\":\"https:\/\/ampscript.guide\/scope\/\",\"name\":\"Scope\"}}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\",\"name\":\"dev\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","_links":{"self":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31302"}],"collection":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/comments?post=31302"}],"version-history":[{"count":0,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31302\/revisions"}],"wp:attachment":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/media?parent=31302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/categories?post=31302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/tags?post=31302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}