> 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/overview.md).

# Overview & Quick Start

Evaluate FHIRPath 2.0 expressions against FHIR data.

FHIRPath is a query language for FHIR data, much like XPath for the resource tree. Use it to pull fields, filter collections, and test conditions without walking PHP objects by hand. This component is a FHIRPath 2.0 evaluator: path navigation, filtering, aggregation, a library of built-in functions, and a FHIR-aligned type system with `is`/`as` support.

The public entry point is `FHIRPathService` (`Ardenexal\FHIRTools\Component\FHIRPath\Service\FHIRPathService`), which wraps lexing, parsing, and evaluation behind three methods: `evaluate()`, `compile()`, and `validate()`.

## Quick start

```php
use Ardenexal\FHIRTools\Component\FHIRPath\Service\FHIRPathService;

$service = new FHIRPathService();

// Evaluate against FHIR data — always returns a Collection
$result = $service->evaluate('Patient.name.given', $patient);

// Filtering and projection
$result = $service->evaluate('name.where(use = "official").given.first()', $patient);

// Boolean check
$hasPhone = $service->evaluate('telecom.where(system = "phone").exists()', $patient);
```

`evaluate()` returns a `Ardenexal\FHIRTools\Component\FHIRPath\Evaluator\Collection` — FHIRPath is collection-centric, so even scalar-looking results are wrapped in a collection.

The signature accepts optional arguments:

```php
public function evaluate(
    string $expression,
    mixed $resource,
    ?EvaluationContext $context = null,
    ?string $fhirVersion = null,   // 'R4' | 'R4B' | 'R5' hint for typed functions
    bool $strictMode = false       // enables runtime semantic validation
): Collection
```

### Validation

`validate()` parses without evaluating and returns a `bool`:

```php
$service->validate('name.given');     // true
$service->validate('name.given.???'); // false
```

### Error handling

All errors extend `Ardenexal\FHIRTools\Component\FHIRPath\Exception\FHIRPathException` (syntax, token, parse, evaluation, and semantic exceptions all derive from it).

```php
use Ardenexal\FHIRTools\Component\FHIRPath\Exception\FHIRPathException;

try {
    $result = $service->evaluate('invalid..path', $patient);
} catch (FHIRPathException $e) {
    echo "FHIRPath error: {$e->getMessage()}";
}
```

## What's covered

* [Expressions & Operators](/fhirpath/expressions.md)
* [Function Reference](/fhirpath/functions.md) — grouped by category
* [Compilation, Caching & Performance](/fhirpath/performance.md)
* [Implementation Status & Known Issues](/fhirpath/status.md)


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
