PHP Feature Flags Quickstart
This guide gets you from zero to evaluating a feature flag in a PHP application. The SDK uses PSR-compatible interfaces and works with any framework.
Prerequisites
Section titled “Prerequisites”- A Featureflip account with at least one feature flag created
- An SDK key from your environment settings
- PHP 8.2+
- Composer
Install
Section titled “Install”composer require featureflip/featureflip-php guzzlehttp/guzzle symfony/cacheThis installs the SDK along with Guzzle (PSR-18 HTTP client and PSR-17 factories) and Symfony Cache (PSR-16 cache).
Initialize the client
Section titled “Initialize the client”<?php
require __DIR__ . '/vendor/autoload.php';
use Featureflip\FeatureflipClient;use Featureflip\Config;use GuzzleHttp\Client as Guzzle;use GuzzleHttp\Psr7\HttpFactory;use Symfony\Component\Cache\Psr16Cache;use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new Psr16Cache(new FilesystemAdapter());$guzzle = new Guzzle();$factory = new HttpFactory();
$config = new Config( baseUrl: 'https://eval.featureflip.io', cache: $cache, httpClient: $guzzle, requestFactory: $factory, streamFactory: $factory,);
$client = FeatureflipClient::get('your-sdk-key', $config);The factory fetches your flag configuration on startup (if the cache is expired) and registers a shutdown function to flush events automatically.
Evaluate a flag
Section titled “Evaluate a flag”$showBanner = $client->boolVariation('new-banner', ['user_id' => 'user-123'], false);
if ($showBanner) { echo "Showing the new banner";} else { echo "Using the default experience";}Each variation method is typed to its return value. The default argument is returned when the flag is not found or evaluation fails.
$color = $client->stringVariation('banner-color', ['user_id' => 'user-123'], 'blue');$limit = $client->numberVariation('rate-limit', ['user_id' => 'user-123'], 100);Clean up
Section titled “Clean up”The SDK registers a shutdown function that flushes pending analytics events when the PHP process exits. You can also flush or close manually:
$client->flush(); // Send buffered events immediately$client->close(); // Flush events and finish the request (PHP-FPM)For long-running processes, call $client->close() when your application shuts down.
How it works
Section titled “How it works”The PHP SDK is a server-side SDK that evaluates flags locally in your PHP process. On initialization, it fetches the full flag configuration from the Featureflip evaluation API and caches it using your PSR-16 cache implementation. Subsequent requests within the cache TTL (5 minutes by default) use the cached configuration without making API calls. Because evaluation happens locally, boolVariation() and other variation calls return immediately.
Unlike long-running SDK processes in other languages, PHP’s request lifecycle means the SDK fetches fresh configuration when the cache expires rather than polling continuously.
Troubleshooting
Section titled “Troubleshooting”Flags are stale or do not update after changes in the dashboard
The PHP SDK caches flag configuration using the PSR-16 cache you provide. If you use a file-based cache, changes to flags take effect once the cache TTL expires (5 minutes by default). For faster updates, reduce the cache TTL or switch to an in-memory cache like Redis.
FeatureflipClient::get() throws an HTTP error
Verify your SDK key and that eval.featureflip.io is reachable. If you are using a custom HTTP client, ensure it supports HTTPS and follows redirects. The baseUrl must include the protocol (https://).
InvalidArgumentException from the cache layer
PSR-16 cache keys have reserved characters ({}()/\@:). The SDK handles this internally, but if you see this error, ensure you are using a PSR-16 compliant cache implementation. Symfony Cache and Laravel’s cache are both compatible.
Next steps
Section titled “Next steps”- PHP SDK reference — full API documentation
- Targeting rules — deliver different values to different users
- Rollout strategies — gradually release features with percentage rollouts
- How to Create Feature Flags — create and manage flags from the dashboard
- Environments — manage separate configurations for dev, staging, and production