Snippet da Semana!

#67 - Simple A/B variation class for experiments

Data: 2019-06-29 12:00 - PHP

Class to do variation testing based on experiments and number of variations in PHP.

<?php
class Experiment {
    public static function getVariation($experiment, $numVariations = 2) {
        return hexdec(substr(sha1($experiment . '.' . ($_SERVER['REMOTE_ADDR'] ?? '') . '.' . ($_SERVER['HTTP_USER_AGENT'] ?? '')), 0, 4)) % $numVariations;
    }
}

Previous snippet | Next snippet