PHP code example of flagdash / sdk

1. Go to this page and download the library: Download flagdash/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

flagdash / sdk example snippets


use FlagDash\Client;

$client = new Client($_ENV['FLAGDASH_SDK_KEY']);

if ($client->flag('checkout-v2', false, ['user_id' => 'alice'])) {
    // new checkout
}

$client->close();

$client = new Client(
    sdkKey: $_ENV['FLAGDASH_SDK_KEY'],
    baseUrl: 'https://flagdash.io',  // self-hosted? point it here
    timeout: 5.0,                    // seconds per request
    cacheTtl: 60.0,                  // seconds; 0 disables caching
    region: 'eu-west-1',             // omit to auto-detect
    transport: new StreamTransport(),
);

// A single flag, with a fallback used whenever FlagDash cannot be reached.
$enabled = $client->flag('checkout-v2', false, ['user_id' => 'alice']);

// Every flag for this context, in one request.
$flags = $client->allFlags(['user_id' => 'alice', 'country' => 'GB']);

// Why did it resolve that way? (server key)
$detail = $client->flagDetail('checkout-v2', false, ['user_id' => 'alice']);
// ['value' => true, 'reason' => 'rule_match', 'variation' => 'treatment', ...]

// Flag metadata without evaluating anything (server key).
$all = $client->listFlags();

$client->flag('beta-banner', false, [
    'user_id' => 'alice',
    'country' => 'GB',
    'plan'    => 'premium',
    'email'   => '[email protected]',
]);

$limit = $client->config('rate_limit', 100);
$all   = $client->listConfigs();

$prompt = $client->aiConfig('support-agent.md');
$files  = $client->listAiConfigs();

$greeting = $client->translation('checkout.greeting', 'fr', 'Hello', [
    'name' => 'Alice',
]);

$assignment = $client->experiment('checkout-redesign', ['user_id' => 'alice']);

if ($assignment !== null && $assignment['variant'] === 'treatment') {
    // ...
}

$client->trackExperimentMetric(
    experimentKey: 'checkout-redesign',
    eventName: 'purchase',
    userId: 'alice',
    value: 42.50,
    properties: ['currency' => 'GBP'],
);

$client->flush();  // send now
$client->close();  // flush and release the transport

$client->clearCache();

$replay = new FlagDash\BackendReplay($_ENV['FLAGDASH_REPLAY_KEY'], release: '2026.08');
if ($replay->start()) {
    $replay->event('checkout_started', attributes: ['items' => 2]);
    $replay->breadcrumb('payment requested');
    $replay->stop();
}

$release = $client->aiConfigRelease("support-agent", "usr_123");