Technical References - [if]

This context displays HTML or executes WebDNA conditionally only if the expression is true.

numero = 241
interpreted = N
texte = [if Expressions][then]do this[/then][else]otherwise this[/else][/if] To display some HTML (or execute WebDNA [xxx] tags) only if certain conditions are met, place the text inside an [If] context. The comparison, which may contain any [xxx] tags, is first evaluated to see if it is true, and if true then the contained [then] context is executed (or simply displayed, if it's just HTML). If not true, then the contained [else] context is executed (or simply displayed, if it's just HTML). Example:
[if (("[username]"="Grant") | ([grandTotal]<100)) & ({[date]}<{2/15/2000})]    [then]either username was Grant or grandTotal was < $100 and it's not Feb 15, 2000 yet[/then]    [else]The complex expression wasn't true[/else][/if]

A common mistake is to omit the "" around text variables

Example
[if (("apples"="red")&("bananas"="yellow"))|(200>100)][then]This is correct[/then][else]This is wrong[/else][/if]
Comparisons are always case-insensitive so "grant" equals "GRANT". The expression is evaluated as a mathematical boolean equation, where each sub-expression evaluates to either 0 or 1 (meaning true or false). If the entire evaluated expression is true, then the WebDNA inside the [then] context is executed, otherwise the [else] context is executed. The [math] context has been extended to allow for quoted text and boolean operators, and is actually what is used by [if] to perform the work of evaluating the expression. A side-effect of this allows you to use these operators inside a [math] equation: [math]1<3[/math] evaluates to "1", because the equation is true. Conversely, [math]3<1[/math] evaluates to "0" because the equation is false. Similarly, [math]1&1[/math] evaluates to "1", and [math]1&0[/math] evaluates to "0".
ComparisonExample
equal =[If "[username]" = "SAGEHEN"] variable [username] is equal to SAGEHEN
not equal ![If [random] ! 45] random number is not 45
contains ^[If "[browsername]" ^ "Mozilla"] variable [browsername] contains the text Mozilla
begins with ~[If "[ipaddress]" ~ "245.078.013"] variable [ipaddress] begins with 245.078.013

Notice the IP address has been typed with 3 digits in each portion ofthe address. this is very important for making these comparison workas expected.
less than <[If [random] < 50] random number is less than 50
greater than >[If [lastrandom] > 25] last random number is greater than 25
divisible by\[If [index] \ 3] variable [index] is divisble by 3
or|[If (5>4) | (1<3)] Boolean comparison: if either side of the operatoris true, then the comparison is true
and&[If (5>4) & (1<3)] Boolean comparison: if both sides of theoperator are true, then the comparison is true
Delimiter Example
Quoted Text
"..."[If "Hello" ^ "hell"] All text must be surrounded by quotes
Numbers [If 12.5 < 13.2] Numbers do not need to be delimited; they function the same as in a [Math] context
Dates{}[If {[date]} > {9/7/1963}] Dates must be enclosed in curly braces to distinguish them from regular numbers
Times{}[If {[time]} > {12:31:00PM} Times must be enclosed in curly braces to distinguish them from regular numbers
Parentheses(...)[If (3>1) & ("a"<"b")] You may collect groupsof items in parentheses in order to force the order of evaluation
Example: An example to detect mobilephones:
[if ("[BROWSERNAME]"="Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20") | ("[BROWSERNAME]"="BlackBerry8330/4.3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/126 UP.Browser/5.0.3.3")][then][REDIRECT url=mobile/index.html][/then][else][/else][/if]
An example with some handy techniques to use for dealing with boolean values, by Brian Fries: I define two global text variables in my header include files:
[text]true=1=1[/text][text]false=1=0[/text]
Then I use these to set my "boolean" variables within my code:
[text]needToDoThis=[true][/text][text]alreadyDidThis=[true][/text]
Then in my IF, SHOWIF and HIDEIF statements, I can use the following:
[if [needToDoThis]][then]do this[/then][else]don't do this[/else][/if][showif [needToDoThis]]do this[text]alreadyDidThis=[true][/text][/showif][hideif [alreadyDidThis]]gotta do this[/hideif]

When using some comparisons such as ">" or "<", make sure you have a valid string or the statement will always issue the exception (ie [else][/else]). In the example below, because the first comparison now has something to compare, the statement is not "nulled" out and the second comparison is allowed to make the whole statement true.

This won't work:When "balance" = nothing[text]balance=[/text][if ([balance]>0) | ("[balance]" = "")]  [then]    should do this  [/then][/if]This will work:When "balance" = nothing[text]balance=[/text][if (0[balance]>0) | ("[balance]" = "")]  [then]    should do this  [/then][/if]
This won't work:[if [number]=11|16][then]do this[/then][else]do that[/else][/if]this will work[if ([number]=11)|([number]=16)][then]do this[/then][else]do that[/else][/if]
[if Expressions][then]do this[/then][else]otherwise this[/else][/if]

To display some HTML (or execute WebDNA [xxx] tags) only if certain conditions are met, place the text inside an [if] context. The comparison, which may contain any [xxx] tags, is first evaluated to see if it is true, and if true then the contained [then] context is executed (or simply displayed, if it's just HTML). If not true, then the contained [else] context is executed (or simply displayed, if it's just HTML).

Example:
[if (("[username]"="Grant") | ([grandTotal]<100)) & ({[date]}<{2/15/2000})]
[then]either username was Grant or grandTotal was < $100 and it's not Feb 15, 2000 yet[/then]
[else]The complex expression wasn't true[/else]
[/if]


A common mistake is to omit the "" around text variables


Example

[if (("apples"="red")&("bananas"="yellow"))|(200>100)]
[then]This is correct[/then]
[else]This is wrong[/else]
[/if]

Comparisons are always case-insensitive so "grant" equals "GRANT". The expression is evaluated as a mathematical boolean equation, where each sub-expression evaluates to either 0 or 1 (meaning true or false). If the entire evaluated expression is true, then the WebDNA inside the [then] context is executed, otherwise the [else] context is executed.

The [math] context has been extended to allow for quoted text and boolean operators, and is actually what is used by [if] to perform the work of evaluating the expression. A side-effect of this allows you to use these operators inside a [math] equation: [math]1<3[/math] evaluates to "1", because the equation is true. Conversely, [math]3<1[/math] evaluates to "0" because the equation is false. Similarly, [math]1&1[/math] evaluates to "1", and [math]1&0[/math] evaluates to "0".

ComparisonExample
equal =[If "[username]" = "SAGEHEN"] variable [username] is equal to SAGEHEN
not equal ![If [random] ! 45] random number is not 45
contains ^[If "[browsername]" ^ "Mozilla"] variable [browsername] contains the text Mozilla
begins with ~[If "[ipaddress]" ~ "245.078.013"] variable [ipaddress] begins with 245.078.013

Notice the IP address has been typed with 3 digits in each portion ofthe address. this is very important for making these comparison workas expected.
less than <[If [random] < 50] random number is less than 50
greater than >[If [lastrandom] > 25] last random number is greater than 25
divisible by\[If [index] \ 3] variable [index] is divisble by 3
or|[If (5>4) | (1<3)] Boolean comparison: if either side of the operatoris true, then the comparison is true
and&[If (5>4) & (1<3)] Boolean comparison: if both sides of theoperator are true, then the comparison is true
Delimiter Example
Quoted Text
"..."[If "Hello" ^ "hell"] All text must be surrounded by quotes
Numbers [If 12.5 < 13.2] Numbers do not need to be delimited; they function the same as in a [math] context
Dates{}[If {[date]} > {9/7/1963}] Dates must be enclosed in curly braces to distinguish them from regular numbers
Times{}[If {[time]} > {12:31:00PM} Times must be enclosed in curly braces to distinguish them from regular numbers
Parentheses(...)[If (3>1) & ("a"<"b")] You may collect groupsof items in parentheses in order to force the order of evaluation


Example:

An example to detect mobilephones:
[if ("[browsername]"="Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20") | ("[browsername]"="BlackBerry8330/4.3.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/126 UP.Browser/5.0.3.3")]
[then][REDIRECT url=mobile/index.html][/then]
[else][/else]
[/if]


An example with some handy techniques to use for dealing with boolean values, by Brian Fries:

I define two global text variables in my header include files:
[text]true=1=1[/text]
[text]false=1=0[/text]

Then I use these to set my "boolean" variables within my code:
[text]needToDoThis=[true][/text]
[text]alreadyDidThis=[true][/text]

Then in my IF, SHOWIF and HIDEIF statements, I can use the following:
[if [needToDoThis]][then]
do this
[/then][else]
don't do this
[/else][/if]

[showif [needToDoThis]]
do this
[text]alreadyDidThis=[true][/text]
[/showif]

[hideif [alreadyDidThis]]
gotta do this
[/hideif]


When using some comparisons such as ">" or "<", make sure
you have a valid string or the statement will always issue the exception (ie [else][/else]).
In the example below, because the first comparison now has something to compare, the statement is not "nulled" out and the second comparison is allowed to make the whole statement true.


This won't work:
When "balance" = nothing
[text]balance=[/text]

[if ([balance]>0) | ("[balance]" = "")]
[then]
should do this
[/then]
[/if]

This will work:
When "balance" = nothing
[text]balance=[/text]

[if (0[balance]>0) | ("[balance]" = "")]
[then]
should do this
[/then]
[/if]


This won't work:
[if [number]=11|16]
[then]do this[/then]
[else]do that[/else]
[/if]

this will work
[if ([number]=11)|([number]=16)]
[then]do this[/then]
[else]do that[/else]
[/if]

DOWNLOAD WEBDNA NOW!

Top Articles:

Technical Change History

This Technical Change History provides a reverse chronological list of WebDNA changes...

F.A.Q

A compilation of some user's questions...

[biotype]

BioType is a behavioral biometrics WebDNA function based on ADGS research and development (from version 8...

WebDNA Modules

A list of the currently available modules...

WebDNA Libraries

A list of available libraries for WebDNA...

Tips and Tricks

A list of user-submitted tips ...

Related Readings:

[cookie]

Cookies are a great way to remember visitors...

[writefile]

[writefile] functions allows you to perform a wide variety of tasks...

[movefolder]

Move a folder and all its contenton your webspace...

[scope]

Explicitly define a block of WebDNA code that has a separate variable space...

[return]

Explicitly identify what text is returned from a function call...

[fileinfo]

Displays information about a particular file or folder...