Skip to main content

Null and Undefined

iXML distinguishes between three "empty" states that are often confused:

StatetypeofMeaning
Undefined'undefined'Variable has never been declared — it has neither type nor value
Null'null'Variable is defined but holds no data
Empty string'string'Variable is defined and holds a string with zero length

Null

The NULL type represents a variable that has been defined but holds no data. Set a variable to null explicitly with the null command:

XML
<null var="x" />
<typeof var="x" var_result="t" />
<output>$t</output>
<!-- null -->

NULL is the typical result when a database lookup returns no rows or when an optional value is absent.

Undefined

An undefined variable has never been assigned any value. It has neither type nor value:

XML
<typeof var="neverSet" var_result="t" />
<output>$t</output>
<!-- undefined -->

You can make a defined variable undefined again with unset:

XML
<set var="x">hello</set>
<unset var="x" />
<typeof var="x" var_result="t" />
<output>$t</output>
<!-- undefined -->

Comparison behavior

In loose comparison, undefined, NULL, FALSE, and '' (empty string) all compare equal to each other:

XML
<if value1="$undefined_var" func="=" value2="">
<output>undefined equals empty string</output>
</if>
<!-- undefined equals empty string -->

This means you cannot distinguish between these states using <if> alone. Use typeof for precise type checking:

XML
<typeof var="x" var_result="t" />
<if value1="$t" func="=" value2="null">
<output>x is explicitly null</output>
</if>

Neutral literal behavior

When an uninitialized (undefined) variable is used in a typed context, it defaults to the neutral literal of that type:

  • In arithmetic context: 0
  • In string context: ''
  • In boolean context: FALSE

This allows operations on uninitialized variables without errors, but can mask bugs if you expect a variable to have been set.