ODBiC Discussion Board


RE: <!--DEFAULT $var(...) similar to <!--SET $var(...), Tony Klett, 10-15-2004

DEFAULT doesn't create a variable

by Roger Harris, October 19, 2004 10:17

DEFAULT is just a way of specifying a value for variable, which is used only if the variable doesn't already have a value when it's referenced. It avoids having to write code like this (although it isn't really the same thing):

<%
IF NOT $var$;
    SET var="value";
ENDIF;
%>
But it isn't quite the same as that code, because it still doesn't set the variable to that value. Instead, each time that a variable is referenced in an output line, if the variable is undefined or its value is an empty string, then the DEFAULT value is output instead. So, it's really more like using the "inline if" function, like this:
$if($var$,$var$,"value")
The $if function tests the "logical" value of the first expression, and if it's "true" then the second expression is returned as the value of the function; otherwise the third expression is returned. In the above line, there aren't any comparison operators in the first expression -- just the single variable reference $var$ -- so the expression is true if it evaluates to a non-empty and non-zero value. In the above example, that's just the $var$ value itself. If the variable is undefined, or it is an empty string, or if it has a numeric value of 0, then the condition is false, so the third expression is returned. In the above example, that's just the literal string "value" but it can be any expression. The DEFAULT statement is just a "shorthand" way of doing the same thing without having to use $if( ) each time the variable is referenced. However, depending on what you need to do, you may really want to use an IF statement and explicitly SET a value, or you may need to use an $if( ) function to default a value in some references but not others.

Replies:


Post Your Reply:

Name   
E-mail  optional
Topic  
Message:

HTTP Link: 
Link text: 


Copyright ©1997-2003, Roger Harris. All rights reserved.