Operators
Operators perform specific operations on values in Expression Language. They enable:
- Calculations
- Comparisons
- Logical decisions
- String manipulation
- Data access
This reference covers every available operator with syntax examples and common usage patterns.
Arithmetic Operators
Mathematical operations for calculations and numeric transformations.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
+ | Addition | a + b | 5 + 3 | 8 | Add numbers, calculate totals |
- | Subtraction | a - b | 10 - 4 | 6 | Subtract values, find differences |
* | Multiplication | a * b | 6 * 7 | 42 | Multiply numbers, calculate percentages |
/ | Division | a / b | 15 / 3 | 5 | Divide numbers, find averages |
% | Modulus | a % b | 17 % 5 | 2 | Find remainder, check even/odd |
** | Exponentiation | a ** b | 2 ** 3 | 8 | Calculate powers, exponential growth |
Practical examples:
# Calculate total price including tax rate
[[price + (price * tax_rate)]]
# Apply percentage discount to original price
[[original_price - (original_price * discount_percentage / 100)]]
# Alternate CSS classes for table rows
[[index % 2 == 0 ? 'even-row' : 'odd-row']]
# Calculate compound interest over time
[[principal * (1 + rate) ** years]]
Comparison Operators
Compare values and return boolean results for conditional logic.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
== | Equal | a == b | 5 == '5' | true | Loose equality, type conversion |
=== | Identical | a === b | 5 === '5' | false | Strict equality, no type conversion |
!= | Not Equal | a != b | 5 != 3 | true | Loose inequality |
!== | Not Identical | a !== b | 5 !== '5' | true | Strict inequality |
< | Less Than | a < b | 3 < 5 | true | Numeric and alphabetical comparison |
> | Greater Than | a > b | 10 > 5 | true | Numeric and alphabetical comparison |
<= | Less Than or Equal | a <= b | 5 <= 5 | true | Inclusive comparison |
>= | Greater Than or Equal | a >= b | 10 >= 8 | true | Inclusive comparison |
Practical examples:
# Display different text based on post status
[[post_status == 'publish' ? 'Live' : 'Draft']]
# Check if price falls within acceptable range
[[price >= min_price and price <= max_price]]
# Set user category based on age threshold
[[user_age >= 18 ? 'Adult' : 'Minor']]
# Show availability based on inventory count
[[inventory > 0 ? 'In Stock' : 'Out of Stock']]
Logical Operators
Combine and manipulate boolean values for complex conditional logic.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
and / && | Logical AND | a and b | true and false | false | Both conditions must be true |
or / || | Logical OR | a or b | true or false | true | At least one condition must be true |
not / ! | Logical NOT | not a | not true | false | Negates/reverses boolean value |
Truth tables and examples:
# Require all conditions to be met
[[is_logged_in and has_permission and is_active]]
# Allow access if user has any qualifying role
[[is_admin or is_editor or is_moderator]]
# Check for opposite condition
[[not is_guest_user]]
# Complex permission checking with grouped conditions
[[is_logged_in and (is_admin or is_editor) and not is_suspended]]