This post was updated 592 days ago and some of the ideas may be out of date.

将多个常量通过按位或 | 操作符合并为一个整数。然后在函数中通过按位与 & 运算符检查该整数是否包含某个常量。

<?php

/**
 * Created by PhpStorm.
 * User: LinFei
 * Created time 2023/04/15 15:22:34
 * E-mail: fly@eyabc.cn
 */
declare (strict_types=1);


class Test
{
    const CHECK_A = 1; // 二进制 0001
    const CHECK_B = 2; // 二进制 0010
    const CHECK_C = 4; // 二进制 0100
    const CHECK_D = 8; // 二进制 1000

    public static function check(int $value, int $checkType = self::CHECK_A): bool
    {
        if ($checkType & self::CHECK_A && $value < 10) {
            return false;
        }

        if ($checkType & self::CHECK_B && $value > 100) {
            return false;
        }

        if ($checkType & self::CHECK_C && $value < 1000) {
            return false;
        }

        if ($checkType & self::CHECK_D && $value > 10000) {
            return false;
        }

        return true;
    }
}

var_dump(Test::check(101, Test::CHECK_A | Test::CHECK_B));