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]]
String Operators
Manipulate and combine text values.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
~ | Concatenation | a ~ b | 'Hello' ~ ' World' | 'Hello World' | Join strings together |
Practical examples:
# Combine first and last name with space
[[first_name ~ ' ' ~ last_name]]
# Build dynamic URL from base and page number
[[base_url ~ '/page/' ~ page_number]]
# Format price display with currency symbol
[['Price: $' ~ price ~ ' (' ~ currency ~ ')']]
# Add draft indicator to unpublished posts
[[post_title ~ (post_status != 'publish' ? ' [DRAFT]' : '')]]
Even if you concatenate non-strings, the output will be a string. For example 1 ~ 2 evaluates to "12" not 12. This means you also cannot concatenate dynamic data, for eg. wp. ~ .post ~ .title will render as wp.post.title not Hello World!
Array Operators
Test array membership and create ranges.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
in | Contains | value in array | 'red' in ['red', 'blue'] | true | Check if value exists in array |
not in | Does Not Contain | value not in array | 'green' not in ['red', 'blue'] | true | Check if value missing from array |
.. | Range | start..end | 1..5 | [1,2,3,4,5] | Generate numeric sequences |
Practical examples:
# Check if post has any acceptable status
[[post_status in ['publish', 'future', 'private']]]
# Exclude posts from banned or inactive users
[[user_role not in ['banned', 'suspended', 'inactive']]]
# Validate current page within total page count
[[current_page in 1..total_pages]]
# Grant access based on user role membership
[[user_role in ['admin', 'editor'] ? 'Edit Access' : 'Read Only']]
Access Operators
Navigate object properties and array elements.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
. | Property Access | object.property | user.display_name | Property value | Access object properties |
[] | Array Access | array[index] | posts[0] | Array element | Access array elements by index |
Practical examples:
# Access nested user information from post
[[wp.post.post_author.display_name]]
# Get name from first category in array
[[categories[0].name]]
# Access featured image URL property
[[featured_image.file_url]]
# Get title from first post in query results
[[wp.posts_query.posts[0].post_title]]
Conditional Operators
Make decisions and provide fallback values.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
? : | Ternary | condition ? true_val : false_val | age >= 18 ? 'Adult' : 'Minor' | Conditional result | If/then/else logic |
?? | Null Coalescing | value ?? fallback | custom_title ?? post_title | First non-null value | Provide fallback values |
Practical examples:
# Show personalized greeting based on login status
[[is_logged_in ? 'Welcome back!' : 'Please log in']]
# Handle multiple post states with nested conditions
[[post_status == 'publish' ? 'Published' : (post_status == 'draft' ? 'Draft' : 'Other')]]
# Use first available content source
[[custom_excerpt ?? post_excerpt ?? limit_words(post_content, 30)]]
# Display price or unavailable message with formatting
[[(inventory > 0 and price > 0) ? ('$' ~ price ~ ' - Available') : 'Out of Stock']]
Specialized Operators
Advanced operators for specific use cases.
| Operator | Name | Syntax | Example | Result | Usage |
|---|---|---|---|---|---|
matches | Regex Match | string matches pattern | email matches '/^.+@.+$/' | true/false | Pattern matching validation |
& | Bitwise AND | a & b | 5 & 3 | 1 | Binary operations (advanced) |
| | Bitwise OR | a | b | 5 | 3 | 7 | Binary operations (advanced) |
-> | Arrow Function | (param) -> expression | (item) -> item.active | Function | Callback functions for arrays |
Practical examples:
# Validate email format using regex pattern
[[email matches '/^[^@]+@[^@]+\.[^@]+$/' ? 'Valid' : 'Invalid']]
# Detect gallery shortcode in post content
[[post_content matches '/\\[gallery.*?\\]/' ? 'Has Gallery' : 'No Gallery']]
# Filter array to published posts only
[[filter(posts, (post) -> post.post_status == 'publish')]]
# Check permission flags using bitwise operation
[[user_permissions & admin_flag]]
Operator Precedence
Operations are evaluated in specific order. Higher precedence = evaluated first.
| Precedence | Operators | Description |
|---|---|---|
| 500 | + - (unary) | Positive/negative signs |
| 200 | ** | Exponentiation |
| 60 | * / % | Multiplication, division, modulus |
| 40 | ~ | String concatenation |
| 30 | + - | Addition, subtraction |
| 25 | .. | Range |
| 20 | < > <= >= == === != !== in not in matches | Comparisons |
| 15 | and && | Logical AND |
| 10 | or || | Logical OR |
| 5 | -> | Arrow functions |
| 0 | ? : | Ternary conditional |
Precedence examples:
# Multiplication happens before addition
[[2 + 3 * 4]] # Result: 14 (not 20)
# Override precedence with parentheses
[[(2 + 3) * 4]] # Result: 20
# Logical operations follow precedence rules
[[price > 100 and quantity > 0 or is_sale]]
# Evaluates as: (price > 100 and quantity > 0) or is_sale
# Use parentheses for clarity in complex expressions
[[(price > 100 and quantity > 0) or is_sale]]
Common Patterns
Range Checking
[[price >= 50 and price <= 100]]
[[user_age >= 13 and user_age <= 17]]
Status Badges
[[post_status == 'publish' ? 'Live' : 'Draft']]
[[inventory > 0 ? 'Available' : 'Sold Out']]
Safe Operations
# Prevent division by zero
[[total > 0 ? amount / total : 0]]
# Safe string operations
[[title ?? 'Untitled']]
Multiple Conditions
[[is_logged_in and has_permission and not is_banned]]
[[is_sale or (price < 20) or (is_member and price < 50)]]
Common Mistakes
| Mistake | Wrong | Correct | Why |
|---|---|---|---|
| Assignment vs Comparison | status = 'active' | status == 'active' | Use == for comparison |
| String Addition | 'Hello' + 'World' | 'Hello' ~ 'World' | Use ~ for string concatenation |
| Precedence | 5 + 3 * 2 | (5 + 3) * 2 | Use parentheses for clarity |
| Incomplete Ternary | condition ? 'yes' | condition ? 'yes' : 'no' | Ternary needs both values |
| Type Confusion | '5' === 5 | '5' == 5 | Use == for loose comparison |
For data transformation capabilities, see: Built-in Functions → - 100+ functions for processing and transforming data
For complete syntax rules and structure, see: Expression Syntax → - Language structure, data access, and formatting rules
Operators are the building blocks of Expression Language logic. Master these patterns to create sophisticated conditional logic, calculations, and data transformations throughout your Builderius templates.