42 users using DBSP now!
home -> documentation -> language reference -> expression property


Description

This property allows the evaluation of mathematical expressions. The expressions could be of two kinds: arithmetic or Boolean. Variables are also supported inside the expressions. There are two ways to set variables: as parameters passed directly to the page by the URL or variables set before the expression be evaluated using the SETCOOKIE tag.

In addition, if the expression is contained inside a DATASET block, all the field names will become automatically available as variables for its immediate use and the values of these variables will correspond to the values contained in the current record.


Syntax

<expression> ::= <term> { <add operator> <term>}

<term> ::= <factor> { <mult operator> <factor> | <bool operator> <factor> } | <function> | <string>

<factor> ::= <number> | ( <expression> )

<function> ::= <funcname> ( <arguments> )

<funcname> ::= A..Za..z(A..Z,a..z,0..9,_)*

<arguments> ::= <expression> | argument,<expression>

<add operator> ::= + | -

<mult operator> ::= * | / | %

<bool operator> ::= > | < | & | | | = | !=

<number> ::= (0..9)+ | (0..9)+ . (0..9)+

<string> ::= 'any character delimited by quotes except end of line'


 

Applications

This property is commonly used for dynamic calculation inside a DBSP page: sums, formulas, statistics and so on. You can also use a variety of mathematical functions described here.

The expression property is used in the DBSP tags:

SETCOOKIE
EXPVAL
IF
WHILE
FUNCTIONS

Notice that the result of the evaluation and the destiny of the value will depend on the tag and context of the command.

For example, if the expression is used in the EXPVAL tag, the result will be replaced instead the tag.

If the expression is used inside the SETCOOKIE tag, then the resulting value will be set as the value for the referenced cookie.

If the expression is used inside the IF tag or WHILE tag, then the control flow will depend of the Boolean value of the result (True or false).


Code examples

Example 1. Using expressions inside cookies.

This example shows how to calculate arithmetic expressions and store the result into a cookie for later use.

Source code

<html>
<body>
 
<!-- we set two values factor1 and factor2 using expressions!-->
<#setcookie name="factor1" expression="1+5"/>
<#setcookie name="factor2" expression="7-5"/> 
 
<#setcookie name="result" expression="factor1*factor2"/> 
 
the result of the operation : <#factor1/> * <#factor2/> is <#result/> 
<!-- there is another way to calculate and write the value without using a third cookie !-->
<p>another way of calculate the same expression: <#expval expression="factor1*factor2"/></p>
 
<#killcookie name="factor1"/>
<#killcookie name="factor2"/>
<#killcookie name="result"/>
</body>
</html>

Test example


Example 2. Using expressions inside IF tags.

This example shows how to insert a block of code depending on the value of a Boolean expression.

Source code

<html>
<body>
 
<#setcookie name="factor1" expression="1+5"/> 
 
<#setcookie name="factor2" expression="7-5"/> 
 
   <#if expression="factor1>factor2"> 
     the variable factor1 (<#factor1/>) is bigger than the variable factor2 (<#factor2/>)
   <#else>
     the variable factor1 (<#factor1/>) is smaller or same as the variable factor2 (<#factor1/>)
   <#/if>
</body>
</html>

Test example