> For the complete documentation index, see [llms.txt](https://php-fhir-tools.ardenexal.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-fhir-tools.ardenexal.net/fhirpath/expressions.md).

# Expressions & Operators

FHIRPath expressions navigate and transform FHIR data. They combine path navigation, function calls, and operators. Every expression evaluates against an input collection and produces an output collection.

## Operators

The evaluator implements arithmetic, comparison, equivalence, logical, string, collection, membership, and type operators.

| Category        | Operators                        | Notes                                                                  |
| --------------- | -------------------------------- | ---------------------------------------------------------------------- |
| **Arithmetic**  | `+`, `-`, `*`, `/`, `div`, `mod` | `div` is integer division; `mod` is remainder                          |
| **Comparison**  | `=`, `!=`, `<`, `>`, `<=`, `>=`  |                                                                        |
| **Equivalence** | `~`, `!~`                        | Equivalence / non-equivalence (tokens `EQUIVALENT` / `NOT_EQUIVALENT`) |
| **Logical**     | `and`, `or`, `xor`, `implies`    | Three-valued logic                                                     |
| **String**      | `&`                              | String concatenation                                                   |
| **Collection**  | `\|`                             | Union                                                                  |
| **Membership**  | `in`, `contains`                 |                                                                        |
| **Type**        | `is`, `as`                       | Type testing and casting                                               |

{% hint style="info" %}
`~` tests equivalence and `!~` its negation. Equivalence differs from `=` by ignoring collection order and, for strings, case and whitespace.
{% endhint %}

### Examples

```php
// Comparison and logical
$service->evaluate('age > 18 and status = "active"', $patient);
$service->evaluate('value > 100 or value < 0', $observation);

// Arithmetic (precedence: * / div mod bind tighter than + -)
$service->evaluate('value * 2', $observation);
$service->evaluate('(value + 10) / 2', $observation);

// Membership and union
$service->evaluate('"phone" in telecom.system', $patient);
$service->evaluate('name | telecom', $patient);

// Type operators
$service->evaluate('value is Quantity', $observation);
$service->evaluate('value as Quantity', $observation);
```

## Language features

The parser and evaluator support the core FHIRPath 2.0 syntax:

| Feature              | Example                                           |
| -------------------- | ------------------------------------------------- |
| Path navigation      | `Patient.name.given`                              |
| Indexing             | `name[0]`                                         |
| Function calls       | `name.where(use = "official")`                    |
| Literals             | strings, numbers, booleans, date/time, quantities |
| Collection literals  | `{}` (empty), `{1, 2, 3}`                         |
| External constants   | `%context`, `%resource`, …                        |
| Reserved identifiers | `$this`, `$index`, `$total`                       |

### Type system

The `is` operator tests whether an item conforms to a named type, and `as` filters the collection to items of that type. Temporal values use dedicated types (`FHIRPathDate`, `FHIRPathDateTime`, `FHIRPathTime`), and decimals use `FHIRPathDecimal` to preserve precision.

See the [Function Reference](/fhirpath/functions.md) for the full library of built-in functions, including type-conversion helpers such as `toInteger()`, `toQuantity()`, and `ofType()`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://php-fhir-tools.ardenexal.net/fhirpath/expressions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
