Technical References - [if]

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

[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).

[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]

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


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

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".

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


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/")]
[then][REDIRECT url=mobile/index.html][/then]

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:

Then I use these to set my "boolean" variables within my code:

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

[showif [needToDoThis]]
do this

[hideif [alreadyDidThis]]
gotta do this

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

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

This will work:
When "balance" = nothing

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

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

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


Top Articles:

Download WebDNA Applications

WebDNA applications...

Tips and Tricks

A list of user-submitted tips ...

AWS Raw WebDNA LAMP-Plus WebServer

Amazon Web Services (AWS) README for Machine Image ID...


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

WebDNA reference

A list of all WebDNA instructions...

WebDNA Modules

A list of the currently available modules...

Related Readings:


Displays a specific MIME header...


The behavior of this context is to iterate the child XML nodes of a parent node...


Cookies are a great way to remember visitors...




A powerful feature to connect to a TCP port of another computer on the Internet...


[wait] just does what it says...