if

The [if] context displays HTML or executes WebDNA conditionally if the comparisons are true and also an option to display a different output if false . [if] can have multiple comparisons, singularly or as groups.

Example WebDNA code:
[if [qty]=1]
  [then]only one required[/then]
  [else]more than one required[/else]
[/if]

WebDNA [if] Comparisons:
ComparisonModulo OperatorExample
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 of the address, this is very important for making these comparison work as 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 operator is 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

WebDNA [if] Delimiters:
DelimiterOperatorExample
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[math]{}[/math][if [math]{[date]}[/math] > [math]{9/7/1963}[/math]] Dates must be enclosed in curly braces to distinguish them from regular numbers and have the [math] operator applied to convert the date to a number
Times[math]{}[/math][if [math]{[time]}[/math] > [math]{12:31:00PM}[/math]] Times must be enclosed in curly braces to distinguish them from regular numbers and have the [math] operator applied to convert the date to a number
Parentheses(...)[if (3>1) & ("a"<"b")] You may collect groups of items in parentheses in order to force the order of evaluation

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



[if comparison][then]do this[/then][else]otherwise this[/else][/if]

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

Example WebDNA code:


[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 operator is 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[math]{}[/math][if [math]{[date]}[/math] > [math]{9/7/1963}[/math]] Dates must be enclosed in curly braces to distinguish them from regular numbers and have the [math] operator applied to convert the date to a number.
Times[math]{}[/math][if [math]{[time]}[/math] > [math]{12:31:00PM}[/math] Times must be enclosed in curly braces to distinguish them from regular numbers and have the [math] operator applied to convert the date to a number
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:

Example WebDNA code:
[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:
Example WebDNA code:
[text]true=1=1[/text]
[text]false=1=0[/text]


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


Then in my if, showif and hideif statements, I can use the following:
Example WebDNA code:
[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.

Example WebDNA code:
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]
Example WebDNA code:
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]