PHP Integration Guide

PHP Feature Flags

Feature flags for PHP — simple cURL integration for any PHP application

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

# No dependencies needed — uses built-in cURL

3. Evaluate flags

<?php

function get_flag(string $flag_key, array $context = []): bool {
    $ch = curl_init('https://flagbit.anethoth.com/api/v1/evaluate');
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 5,
        CURLOPT_HTTPHEADER => [
            'Content-Type: application/json',
            'X-SDK-Key: ' . getenv('FLAGBIT_SDK_KEY'),
        ],
        CURLOPT_POSTFIELDS => json_encode([
            'flag_key' => $flag_key,
            'context' => $context ?: new stdClass(),
        ]),
    ]);
    $response = curl_exec($ch);
    curl_close($ch);

    if ($response === false) return false;
    $data = json_decode($response, true);
    return $data['value'] ?? false;
}

// With APCu caching
function get_flag_cached(string $flag_key, array $context = [], int $ttl = 60): bool {
    $cache_key = "flag:{$flag_key}:" . md5(json_encode($context));
    $cached = apcu_fetch($cache_key, $success);
    if ($success) return $cached;

    $value = get_flag($flag_key, $context);
    apcu_store($cache_key, $value, $ttl);
    return $value;
}

// WordPress integration
function flagbit_shortcode($atts) {
    $flag_key = $atts['flag'] ?? '';
    if (get_flag_cached($flag_key, ['user_id' => get_current_user_id()])) {
        return $atts['content'] ?? '';
    }
    return '';
}
add_shortcode('feature', 'flagbit_shortcode');

// Symfony controller
class ProductController extends AbstractController {
    #[Route('/products')]
    public function list(): Response {
        $user = $this->getUser();
        $ctx = $user ? ['user_id' => (string)$user->getId()] : [];

        if (get_flag_cached('new-product-list', $ctx)) {
            return $this->render('product/list_v2.html.twig');
        }
        return $this->render('product/list.html.twig');
    }
}

Use Cases for PHP

WordPress Plugins

Gate plugin features behind flags — enable for specific users or roles without code changes.

WooCommerce

A/B test checkout flows, product page layouts, or pricing displays.

Legacy Migration

Gradually migrate from legacy PHP to a modern framework — flag controls which code path runs.

API Versioning

Route API requests to v1 or v2 handlers based on feature flags and user context.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your PHP app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

Yes — the cURL code works with PHP 7.4+. No modern PHP features required.

Use file-based caching, Redis, or Memcached. Any cache that supports TTL will work.

Yes — the shortcode example above lets you feature-gate content in posts and pages.

FlagBit works anywhere cURL is available — shared hosting, VPS, or managed platforms.

Start using feature flags in PHP

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key