{if:etalon:operator:value1:text1:...:else_text}

Description

Numeric conditional. Compares a value (the etalon) against one or more test values using a single comparison operator, and returns the output paired with the first test value that matches. If nothing matches, the trailing else text is returned.

Comparison is always numeric (also for security reasons). The etalon (etalon) and every test value (value1, value2, ...) are converted to a float before comparing: spaces are stripped and a comma is read as the decimal point, so 1 000,5 becomes 1000.5. Any non-numeric string converts to 0 - so comparing two words always sees 0 against 0. For text equality use ifeq instead.

Operators (the etalon is the left-hand side):

  • = == eq - equal
  • <> != ne - not equal
  • > gt - greater than
  • >= ge - greater than or equal
  • < lt - less than
  • <= le - less than or equal

An unknown operator never matches, so the else text is returned. The aliases are ge and le - not gte/lte.

Multiple pairs: after the operator you may chain as many value:text pairs as you like. They are tested top to bottom and the first match wins, like a numeric switch. A final unpaired argument is the else text; if it is omitted and nothing matches, the result is empty.

Placeholders in the output: inside the returned text, _#1 is replaced by the etalon (as a normalized number) and _#2 by the first test value. The output may also contain any other AA expression.

Runtime-dependent: this command is never cached, since it usually compares live values.

Source: AA_Stringexpand_If in include/stringexpand.php.

Parameters

etalon required

The value to test (left-hand side). Usually a number or the result of another expression. It is converted to a float before comparing - a comma is read as the decimal point and spaces are stripped; any non-numeric string becomes 0.

operator required

The comparison operator. Accepted: = == eq (equal), <> != ne (not equal), > gt (greater than), >= ge (greater than or equal), < lt (less than), <= le (less than or equal). The aliases are ge and le, not gte/lte. Any other value never matches, so the else text is returned.

value1 optional

The first test value (right-hand side) compared against the etalon using the operator. Like the etalon it is converted to a float, so it is read numerically. If the comparison is true, text1 is returned.

text1 optional

The output returned when the etalon compared with value1 is true. May be plain text, a number, contain the placeholders _#1 (etalon) and _#2 (value1), or any other AA expression.

value2:text2:... (optional pairs) optional

Optional further value:text pairs after the first one. They are tested top to bottom and the first matching pair wins, like a numeric switch. Add as many pairs as you need before the else text.

else_text optional

The trailing unpaired argument: the output returned when no value matches. Optional - if it is omitted and nothing matches, the result is an empty string. May contain _#1, _#2, or other AA expressions.

Examples

test{if:5:>:3:yes:no}
Expectedyes
Actualyes
The simplest form: one value, one operator, one threshold, one output, and an else. 5 is greater than 3, so the first output wins. A very common real-world use is showing a block only when a count is above zero.
test{if:3:>:10:big:5:medium:small}
Expectedsmall
Actualsmall
Two value:text pairs followed by an else. 3 is not greater than 10 and not greater than 5, so neither pair matches and the trailing else text small is returned.
test{if:7:=:7:equal:different}
Expectedequal
Actualequal
Numeric equality with the = operator (== and eq behave the same). 7 equals 7, so equal is returned.
test{if:7:>:10:high:5:medium:low}
Expectedmedium
Actualmedium
Pairs are tested top to bottom. 7 is not greater than 10, but it is greater than 5, so the second pair matches and medium is returned - the rest is skipped.
test{if:1,5:>:1:yes:no}
Expectedyes
Actualyes
Comparison is always numeric. The comma in 1,5 is read as a decimal point, giving 1.5, which is greater than 1. Spaces in numbers are also stripped, so 1 000 becomes 1000.
test{if:42:>:0:answer is _#1:unknown}
Expectedanswer is 42
Actualanswer is 42
Inside the output text, _#1 is replaced by the etalon as a normalized number, and _#2 by the first test value. Here 42 is greater than 0, so the matched output answer is _#1 becomes answer is 42.