Operators allow handling various scenarios to generate a printing form of a document. The system checks conditions and executes a code based on the result.
This article describes the following operations:
- comparison operator;
- logical operator;
- the if ... else operator;
- conditional operator;
- the switch operator.
This is a binary operator that takes two operands in which values are being compared.
|===||Values are equal.||Equality comparison without data typing. Comparing a digit and a string (the data of different types) always return false.|
|!==||Values are not equal.|
|==||Values are equal.|
Equality comparison with data typing. Comparing a digit and a string (the data of different types) first converts a text into a digit (the same type).
|!=|| Values are not equal.|
|>||The left value is greater than the right one.||It allows defining a larger value.|
|<||The right value is greater than the left one.|
|>=||The left value is greater than or equal to the right one.|
|<=||The right value is greater than or equal to the left one.|
Logical operators combine multiple boolean values and provide a single boolean output.
- && (AND) — this operator will be truthy if the expressions on both sides of it are true.
- || (OR)— this operator will be truthy if the expression on either side of it is true.
- ! (NOT)— this operator converts the operand to boolean type and returns an inverse value.
A rule of logical expression in a code is represented by the keywords true and false. True is returned if a condition is true, and false is returned if a condition is false.
if ... else operator
The use of else is optional:
If one condition may result in several scenarios, you can use else if. The following example shows a condition to display cents in the total amount due:
You can add payer information using if ... else in the standard invoice template:
In the code above, the system checks a payer type. If it is a company, its VAT number and IBAN will be displayed in the document. Otherwise, a customer is considered an individual, and the document will show his address.
A conditional operator is an alternative to if... else. Conditional operators return one value if the condition is true and returns another value if the condition is false.
The system applies the piece of code 1 if the condition is true. If the condition is false, it applies the piece of code 2.
The following function converts a digit into a string:
It can be explained as "if the expression+number is not a finite number, the n variable is assigned 0, otherwise n is assigned +number".
The switch operator excepts a variable or expression and executes a required piece of code depending on the result. Its basic syntax:
The piece of code 1 is applied if the result is 1. The piece of code 2 is applied if the result is 2, and the piece of code 3 if applied if the result is another number except for 1 and 2.
Let's describe the code that returns one of four sentences depending on the input value. This code is not used in a pre-made template in the billing system.
The above code compares the values of the input variable word with possible values:
- if "word" contains the word "hi", the variable"phrase" will be assigned the value "Hello my dear friend";
- if "word" contains "bye", the variable"phrase" will be assigned the value "Goodbye, I hope to see you again";
- if "word" contains "joke", the variable"phrase" will be assigned the value "I don't know jokes";
- if "word" contains "day", the variable"phrase" will be assigned the value "Have a nice day";
- if the values do not match, the variable"phrase" will be assigned the value "I don't understand you".