1. Go to this page and download the library: Download antikirra/probability 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/ */
antikirra / probability example snippets
use function Antikirra\probability;
// Random execution - 30% chance to log debug info
if (probability(0.3)) {
error_log("Debug: processing request #{$requestId}");
}
// Deterministic execution - same user always gets same experience
if (probability(0.5, "new_checkout_user_{$userId}")) {
return renderNewCheckout();
}
// Gradual rollout - increase from 10% to 100% over time
if (probability(0.1, "feature_ai_search_user_{$userId}")) {
enableAISearch();
}
probability(0.25); // 25% chance, different result each time
probability(0.25, 'unique_key'); // Same result for same key
function probability(float $probability, string $key = ''): bool
// 15% random chance
probability(0.15);
// Deterministic 30% for user with id 123
probability(0.30, "user_123");
// Combining feature and user for unique distribution
probability(0.25, "feature_checkout_user_123");
// ❌ Bad - too generic
probability(0.5, "test");
// ✅ Good - specific and unique
probability(0.5, "homepage_redesign_user_$userId");
// ❌ Bad - same users get all features
if (probability(0.2, $userId)) { /* feature A */ }
if (probability(0.2, $userId)) { /* feature B */ }
// ✅ Good - different user groups per feature
if (probability(0.2, "feature_a_$userId")) { /* feature A */ }
if (probability(0.2, "feature_b_$userId")) { /* feature B */ }
// For high-frequency operations, use very small probabilities
if (probability(0.001)) { // 0.1% - suitable for millions of requests
$metrics->record($data);
}
namespace App\Http\Middleware;
use Closure;
use function Antikirra\probability;
class FeatureFlag
{
public function handle($request, Closure $next, $feature, $percentage)
{
$userId = $request->user()?->id ?? $request->ip();
$key = "{$feature}_user_{$userId}";
if (!probability((float)$percentage, $key)) {
abort(404); // Feature not enabled for this user
}
return $next($request);
}
}
// Usage in routes:
// Route::get('/beta', ...)->middleware('feature:beta_dashboard,0.1');
use function Antikirra\probability;
use Psr\Log\LoggerInterface;
class DatabaseQueryLogger
{
public function __construct(
private LoggerInterface $logger,
private float $samplingRate = 0.01 // 1% of queries
) {}
public function logQuery(string $sql, float $duration): void
{
// Random sampling - no need for deterministic behavior
if (!probability($this->samplingRate)) {
return;
}
$this->logger->info('Query executed', [
'sql' => $sql,
'duration' => $duration,
'sampled' => true
]);
}
}
use function Antikirra\probability;
function show_homepage_variant() {
$user_id = get_current_user_id() ?: $_SERVER['REMOTE_ADDR'];
$key = "homepage_redesign_user_{$user_id}";
// 50% of users see new design, consistently
if (probability(0.5, $key)) {
get_template_part('homepage', 'new');
} else {
get_template_part('homepage', 'classic');
}
}
use function Antikirra\probability;
class ApiRateLimiter
{
public function allowRequest(User $user, string $endpoint): bool
{
$limits = [
'free' => 0.1, // 10% of requests allowed
'basic' => 0.5, // 50% of requests allowed
'premium' => 1.0 // 100% of requests allowed
];
$probability = $limits[$user->tier] ?? 0;
$key = "api_{$endpoint}_{$user->id}_" . date('YmdH'); // Hourly bucket
return probability($probability, $key);
}
}
// ❌ This will differ between users
probability(0.5, $userId); // If $userId is different
// ✅ This will be consistent for same user
probability(0.5, "feature_x_user_{$userId}");