Printing a variable only if it exisits

If a variable does not exist, then printing it produces an error. There are several methods of getting around this.

  • We will start by setting $hasval to Has a Value, and not setting $noval
  • Our test line will be: Testing ($noval) and ($hasval)
  1. Suppress error messages (for part or all of document) with $OPT_verbose=-3 (not recommended!)
    e.g. $pushset $OPT_verbose=-3 $test $pop $OPT_verbose
  2. $set $defaultval1=''l results in:
    Testing () and (Has a Value)
  3. Using $if statement, e.g. using the pattern $if $xxx||$xxx||$endif results in:
    Has a Value
  4. Inline conditional statement, e.g Testing ($iftrue $noval $noval '') and ($iftrue $hasval $hasval '') results in:
    Testing () and (Has a Value)
  5. The preferred way using Testing ($noval.whenunset('')) and ($hasval.whenunset('')) results in:
    Testing () and (Has a Value)