---
title: PHP Feature Flags
description: Set up Datadog Feature Flags for PHP applications.
breadcrumbs: Docs > Feature Flags > Server-Side Feature Flags > PHP Feature Flags
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# PHP Feature Flags

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Getting started{% #getting-started %}

Use agentless delivery to receive flag configuration directly from Datadog. The SDK evaluates flags locally from cached configuration.

### Install the SDK{% #install-the-sdk %}

Install the Datadog PHP extension version **1.28.0 or later**. Follow the [PHP library installation instructions](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/php.md). The extension provides the Feature Flags API; an Agent and APM tracing are optional.

Use the Datadog PHP API with PHP 7 or later. For PHP 8 applications, you can also use the [OpenFeature](https://openfeature.dev/) adapter.

### Configure flag delivery{% #configure-flag-delivery %}

Set these environment variables before PHP starts:

```bash
export DD_FEATURE_FLAGS_CONFIGURATION_SOURCE=agentless
export DD_API_KEY='<DATADOG_API_KEY>'
export DD_ENV='<YOUR_ENVIRONMENT>'
```

Set `DD_ENV` to your [Feature Flags environment](https://docs.datadoghq.com/feature_flags/concepts/environments.md). Use a Datadog [API key](https://docs.datadoghq.com/account_management/api-app-keys.md#api-keys). If your organization uses a site other than `datadoghq.com`, set `DD_SITE`. For the site selected on this page, use <YOUR_DATADOG_SITE>.

PHP requires an explicit `DD_FEATURE_FLAGS_CONFIGURATION_SOURCE=agentless` setting. Registering a provider or setting `DD_FEATURE_FLAGS_ENABLED=true` alone does not start configuration delivery. If `DD_FEATURE_FLAGS_ENABLED=false` is set, remove it or set it to `true`.

### Evaluate a flag{% #evaluate-a-flag %}

Create a Boolean flag named `my-flag`. Enable its **Server** distribution channel and enable it in the target environment.

For PHP 7 or PHP 8, create the Datadog client and evaluate the flag:

```php
<?php

use DDTrace\FeatureFlags\Client;

$flags = new Client();
$enabled = $flags->getBooleanValue('my-flag', false);
```

Replace `my-flag` with a flag key from your environment. The example evaluates a global flag without targeting attributes.

For the OpenFeature API on PHP 8, install `open-feature/sdk` version 2.1 or later:

```bash
composer require open-feature/sdk:^2.1
```

Register the Datadog provider and evaluate a flag:

```php
<?php

require_once __DIR__ . '/vendor/autoload.php';

use DDTrace\OpenFeature\DataDogProvider;
use OpenFeature\OpenFeatureAPI;

$api = OpenFeatureAPI::getInstance();
$api->setProvider(new DataDogProvider());
$client = $api->getClient('my-service');
$enabled = $client->getBooleanValue('my-flag', false);
```

Both APIs use the same extension and environment settings. The first evaluation waits up to 10 seconds for initial configuration. If configuration is unavailable, the SDK returns the supplied default value. Later evaluations use available updates without starting another initialization wait.

## Set the evaluation context{% #set-the-evaluation-context %}

Define an evaluation context that identifies the user or entity for flag targeting. The targeting key is used for consistent traffic distribution, such as percentage rollouts. Additional attributes enable targeting rules, such as "enable for users in the US" or "enable for premium tier users."

### Datadog API{% #datadog-api %}

For the Datadog PHP API, pass context as an array with `targetingKey` and `attributes` keys:

```php
$context = [
    'targetingKey' => 'user-123',
    'attributes' => [
        'email' => 'user@example.com',
        'country' => 'US',
        'tier' => 'premium',
        'age' => 25,
    ],
];
```

### OpenFeature adapter{% #openfeature-adapter %}

For the OpenFeature adapter, use the OpenFeature PHP SDK's `EvaluationContext`:

```php
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\implementation\flags\EvaluationContext;

$context = new EvaluationContext(
    'user-123',
    new Attributes([
        'email' => 'user@example.com',
        'country' => 'US',
        'tier' => 'premium',
        'age' => 25,
    ])
);
```

{% alert level="warning" %}
Evaluation context attributes must be flat primitive values: strings, numbers, and Booleans. Nested arrays, objects, and null values are ignored for targeting and exposure reporting.
{% /alert %}

## Evaluate flags{% #evaluate-flags %}

After setting up the client, you can evaluate flags throughout your application. Each flag is identified by a unique string key and evaluated with a typed method that returns a value of the expected type. If the flag does not exist or cannot be evaluated, the SDK returns the provided default value.

### Boolean flags{% #boolean-flags %}

Use `getBooleanValue` for flags that represent on/off or true/false conditions:

```php
$enabled = $flags->getBooleanValue('new-checkout-flow', false, $context);

if ($enabled) {
    showNewCheckout();
} else {
    showLegacyCheckout();
}
```

With OpenFeature:

```php
$enabled = $client->getBooleanValue('new-checkout-flow', false, $context);
```

### String flags{% #string-flags %}

Use `getStringValue` for flags that select between variants or configuration strings:

```php
$theme = $flags->getStringValue('ui-theme', 'light', $context);

switch ($theme) {
    case 'dark':
        setDarkTheme();
        break;
    case 'light':
    default:
        setLightTheme();
        break;
}
```

### Numeric flags{% #numeric-flags %}

For numeric flags, use `getIntegerValue` or `getFloatValue`. These methods are appropriate when a feature depends on a numeric parameter such as a limit, percentage, or multiplier:

```php
$maxItems = $flags->getIntegerValue('cart-max-items', 20, $context);

$discountRate = $flags->getFloatValue('discount-rate', 0.0, $context);
```

### Object flags{% #object-flags %}

For structured data, use `getObjectValue`. This returns a PHP array:

```php
$config = $flags->getObjectValue('feature-config', [
    'maxRetries' => 3,
    'timeout' => 30,
], $context);

$maxRetries = $config['maxRetries'] ?? 3;
$timeout = $config['timeout'] ?? 30;
```

### Flag evaluation details{% #flag-evaluation-details %}

When you need more than just the flag value, use the `get<Type>Details` methods. These return both the evaluated value and metadata explaining the evaluation:

```php
$details = $flags->getBooleanDetails('new-feature', false, $context);

printf("Value: %s\n", $details->getValue() ? 'true' : 'false');
printf("Variant: %s\n", $details->getVariant() ?? 'none');
printf("Reason: %s\n", $details->getReason());

if ($details->isError()) {
    printf("Error Code: %s\n", $details->getErrorCode());
    printf("Error Message: %s\n", $details->getErrorMessage());
}
```

Flag details help you debug evaluation behavior and understand why a user received a given value.

## Complete examples{% #complete-examples %}

The following examples combine initialization, evaluation context, typed evaluation, and evaluation details.

### PHP 7 and PHP 8: Datadog API{% #php-7-and-php-8-datadog-api %}

```php
<?php

use DDTrace\FeatureFlags\Client;

$flags = new Client();

$context = [
    'targetingKey' => 'user-123',
    'attributes' => [
        'country' => 'US',
        'tier' => 'premium',
    ],
];

$details = $flags->getStringDetails('checkout-copy', 'control', $context);

if ($details->isError()) {
    error_log(sprintf(
        'Flag evaluation failed: %s %s',
        $details->getErrorCode(),
        $details->getErrorMessage()
    ));
}

if ($details->getValue() === 'treatment') {
    showTreatmentCopy();
} else {
    showControlCopy();
}
```

### PHP 8: OpenFeature adapter{% #php-8-openfeature-adapter %}

```php
<?php

require_once __DIR__ . '/vendor/autoload.php';

use DDTrace\OpenFeature\DataDogProvider;
use OpenFeature\implementation\flags\Attributes;
use OpenFeature\implementation\flags\EvaluationContext;
use OpenFeature\OpenFeatureAPI;

$api = OpenFeatureAPI::getInstance();
$api->setProvider(new DataDogProvider());

$client = $api->getClient('checkout-service', '1.0.0');
$context = new EvaluationContext(
    'user-123',
    new Attributes([
        'country' => 'US',
        'tier' => 'premium',
    ])
);

$details = $client->getStringDetails('checkout-copy', 'control', $context);
$error = $details->getError();

if ($error !== null) {
    error_log(sprintf(
        'Flag evaluation failed: %s %s',
        $error->getResolutionErrorCode()->getValue(),
        $error->getResolutionErrorMessage()
    ));
}

if ($details->getValue() === 'treatment') {
    showTreatmentCopy();
} else {
    showControlCopy();
}
```

## Evaluation without context{% #evaluation-without-context %}

You can evaluate flags without providing an evaluation context. This is useful for global flags that do not require user-specific targeting:

```php
$maintenanceMode = $flags->getBooleanValue('maintenance-mode', false);

if ($maintenanceMode) {
    http_response_code(503);
    echo 'Service temporarily unavailable';
    return;
}
```

## Additional configuration{% #additional-configuration %}

### Service identification and tracing{% #service-identification-and-tracing %}

Set `DD_SERVICE` and `DD_VERSION` to identify your application in telemetry. These settings are optional for receiving flags.

```bash
export DD_SERVICE=<YOUR_SERVICE_NAME>
export DD_VERSION=<YOUR_APP_VERSION>
```

To add APM tracing, follow [Tracing PHP Applications](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/php.md). For trace annotations, see [APM trace enrichment](https://docs.datadoghq.com/feature_flags/guide/apm_trace_enrichment.md).

### Agent delivery{% #agent-delivery %}

To receive flags through the Datadog Agent, configure the Agent with an [API key](https://docs.datadoghq.com/account_management/api-app-keys.md#api-keys) and enable [Remote Configuration](https://docs.datadoghq.com/agent/remote_config.md). Set these application environment variables:

```bash
export DD_FEATURE_FLAGS_ENABLED=true
export DD_FEATURE_FLAGS_CONFIGURATION_SOURCE=remote_config
export DD_REMOTE_CONFIG_ENABLED=true
export DD_ENV='<YOUR_ENVIRONMENT>'
```

The application API stays the same. Configure service identification as needed for your application.

### Migrate from Agent delivery{% #migrate-from-agent-delivery %}

Set `DD_FEATURE_FLAGS_CONFIGURATION_SOURCE=agentless` and provide `DD_API_KEY` in the PHP process environment. Keep `DD_ENV` matched to your flag environment. If your organization uses a site other than `datadoghq.com`, set `DD_SITE`. Then remove `DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED` and restart PHP workers.

Replacing only `DD_EXPERIMENTAL_FLAGGING_PROVIDER_ENABLED=true` with `DD_FEATURE_FLAGS_ENABLED=true` stops configuration delivery. PHP also requires an explicit source. To retain Agent delivery, set `DD_FEATURE_FLAGS_CONFIGURATION_SOURCE=remote_config`.

Keep Remote Configuration enabled if other Datadog products use it. For the shared migration procedure, see [Migrate an existing Remote Configuration setup](https://docs.datadoghq.com/feature_flags/concepts/configuration_sources.md#migrate-an-existing-remote-configuration-setup).

### Delivery settings{% #delivery-settings %}

The SDK polls for flag updates every 30 seconds. The request timeout is 5 seconds. Configure these values with `DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_POLL_INTERVAL_SECONDS` and `DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_REQUEST_TIMEOUT_SECONDS`.

Set `DD_EXPERIMENTAL_FLAGGING_PROVIDER_INITIALIZATION_TIMEOUT_MS` to change the first-evaluation timeout. Its default is `10000` milliseconds. Credentials, endpoint, and environment settings resolve at process startup. Restart PHP workers after changing them.

Agentless delivery requires the PHP library's subprocess sidecar. The default `auto` sidecar mode uses a subprocess when available. Thread mode, including automatic fallback to thread mode, does not support agentless delivery.

For additional source settings, see [Configuration sources](https://docs.datadoghq.com/feature_flags/concepts/configuration_sources.md).

## Testing{% #testing %}

You can test against a dedicated Datadog test environment with the real Datadog provider, or replace feature flag evaluation with a test double in unit tests.

The OpenFeature PHP SDK 2.1 does not include a built-in in-memory provider. For unit tests, wrap feature flag evaluation behind an application interface and inject a fake implementation:

In the `FeatureFlags.php` file:

```php
<?php

use DDTrace\FeatureFlags\Client;

interface FeatureFlagReader
{
    public function getBooleanValue($flagKey, $defaultValue, array $context = []);
}

final class DatadogFeatureFlagReader implements FeatureFlagReader
{
    private $client;

    public function __construct(Client $client = null)
    {
        $this->client = $client ?: new Client();
    }

    public function getBooleanValue($flagKey, $defaultValue, array $context = [])
    {
        return $this->client->getBooleanValue($flagKey, $defaultValue, $context);
    }
}

final class InMemoryFeatureFlagReader implements FeatureFlagReader
{
    private $flags;

    public function __construct(array $flags)
    {
        $this->flags = $flags;
    }

    public function getBooleanValue($flagKey, $defaultValue, array $context = [])
    {
        return array_key_exists($flagKey, $this->flags)
            ? (bool) $this->flags[$flagKey]
            : $defaultValue;
    }
}
```

Use the fake in your test setup:

In the `CheckoutTest.php` file:

```php
$flags = new InMemoryFeatureFlagReader([
    'new-checkout-flow' => true,
]);

$checkout = new CheckoutService($flags);

self::assertTrue($checkout->usesNewCheckoutFlow('user-123'));
```

## Troubleshooting{% #troubleshooting %}

### Feature flags always return default values{% #feature-flags-always-return-default-values %}

If feature flags unexpectedly always return default values, check the following:

- Verify the PHP extension version is 1.28.0 or later.
- Verify `DD_FEATURE_FLAGS_CONFIGURATION_SOURCE=agentless` is set before PHP starts.
- Verify `DD_FEATURE_FLAGS_ENABLED` is not set to `false`.
- Verify the PHP process receives `DD_API_KEY`, `DD_ENV`, and the correct `DD_SITE`.
- Verify `DD_ENV` matches the environment configured for your flag.
- Check that the PHP sidecar can connect to Datadog and uses subprocess mode.
- Inspect evaluation details to distinguish a returned default from a successful flag evaluation.
- If you select `remote_config`, verify Agent connectivity and Remote Configuration settings. See [Agent availability](https://docs.datadoghq.com/feature_flags/concepts/configuration_sources.md#agent-availability).

### OpenFeature provider not found{% #openfeature-provider-not-found %}

The OpenFeature adapter is available only for PHP 8 applications. If `DDTrace\OpenFeature\DataDogProvider` is not found:

- Verify the application is running on PHP 8 or later.
- Verify `open-feature/sdk` is installed through Composer.
- Verify the Datadog PHP tracer version includes feature flags support.

### Targeting rules do not match{% #targeting-rules-do-not-match %}

If targeting rules do not match as expected:

- Set a stable `targetingKey` for the user, organization, session, or entity being evaluated.
- Pass custom targeting data under the `attributes` key when using the Datadog API.
- Use only flat primitive attributes. Nested arrays, objects, and null values are ignored.
- Verify the `DD_ENV` value appears in [Feature Flag Environments](https://docs.datadoghq.com/feature_flags/concepts/environments.md).

### Verify flag metrics and exposures in Datadog{% #verify-flag-metrics-and-exposures-in-datadog %}

#### Flag evaluation metrics{% #flag-evaluation-metrics %}

The optional `feature_flag.evaluations` metric uses a separate OpenTelemetry (OTLP) export path. Set `DD_METRICS_OTEL_ENABLED=true` and configure an OTLP receiver to use this metric. These settings are not required to receive flags or send experiment exposures and evaluation-count events.

Each evaluation emits a counter tagged with the flag key, result variant, and evaluation reason. For receiver setup and troubleshooting, see [Set Up Server-Side Flag Evaluation Metrics](https://docs.datadoghq.com/feature_flags/guide/server_flag_evaluation_metrics.md). For visualization options, see [Feature Flag Graphs](https://docs.datadoghq.com/feature_flags/concepts/flag_graphs.md).

#### Experiment exposures{% #experiment-exposures %}

Agentless delivery sends experiment exposures and evaluation-count events through a local event proxy when available. Otherwise, the SDK sends these events directly to Datadog using `DD_API_KEY` and `DD_SITE`.

Exposures appear only for flags associated with an experiment. Standard feature flags without an experiment association do not generate exposure events. If exposures are missing:

1. Verify the flag is associated with an experiment in the Datadog UI.
1. For agentless delivery, verify the PHP process has a valid `DD_API_KEY` and the correct `DD_SITE`.
1. Verify the sidecar can reach a local event proxy or the Datadog event intake. With Agent delivery, check the Agent's API key.
1. Use flat primitive attributes in the evaluation context. Nested arrays, objects, and null values are ignored for exposure reporting.

## Further reading{% #further-reading %}

Additional helpful documentation, links, and articles:

- [Server-Side Feature Flags](https://docs.datadoghq.com/feature_flags/server.md)
- [PHP Tracing](https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/php.md)
- [Set Up Server-Side Flag Evaluation Metrics](https://docs.datadoghq.com/feature_flags/guide/server_flag_evaluation_metrics.md)
- [Set Up APM Trace Enrichment for Feature Flags](https://docs.datadoghq.com/feature_flags/guide/apm_trace_enrichment.md)
- [Feature Flag Graphs](https://docs.datadoghq.com/feature_flags/concepts/flag_graphs.md)
