Php 写这些多个elseif的更快方法?

Php 写这些多个elseif的更快方法?,php,Php,有没有一种更简单、更快的方法来写这个 $currTime = date('H:i'); if ($currTime >= "12:09" && $currTime <= "16:08") { echo rand(260, 272); } elseif ($currTime >= "16:09" && $currTime <= "18:08") { echo rand(278, 286); } elseif ($currTim

有没有一种更简单、更快的方法来写这个

$currTime = date('H:i');
if ($currTime >= "12:09" && $currTime <= "16:08") {
    echo rand(260, 272);
} elseif ($currTime >= "16:09" && $currTime <= "18:08") {
    echo rand(278, 286);
} elseif ($currTime >= "18:09" && $currTime <= "20:08") {
    echo rand(293, 303);
} elseif ($currTime >= "20:09" && $currTime <= "23:38") {
    echo rand(338, 359);
} elseif ($currTime >= "23:39" && $currTime <= "23:59") {
    echo rand(293, 302);
} elseif ($currTime >= "01:06" && $currTime <= "02:08") {
    echo rand(195, 210);
} elseif ($currTime >= "02:09" && $currTime <= "02:23") {
    echo rand(168, 179);
} elseif ($currTime >= "02:24" && $currTime <= "07:08") {
    echo rand(121, 128);
} elseif ($currTime >= "07:09" && $currTime <= "09:08") {
    echo rand(143, 160);
} elseif ($currTime >= "09:09" && $currTime <= "12:08") {
    echo rand(187, 207);
} else {
    echo rand(233, 241);
}
$currTime=日期('H:i');

如果($currTime>=“12:09”&&&$currTime=“16:09”&&&$currTime=“18:09”&&&$currTime=“20:09”&&&&$currTime=“23:39”&&&$currTime=“01:06”&$currTime=“02:09”&$currTime=“02:24”&$currTime=“07:09”&$currTime=“09:09”&&&$currTime没有更干净的方法可以做到这一点,switch语句在这种情况下不起作用。您所能做的就是将if稍微缩短一点,但仅此而已。我认为您在这种情况下的方法已经足够好了。

没有更干净的方法可以做到这一点,switch语句在这种情况下不起作用。您所能做的就是使if变短略短一点,但仅此而已。我认为您在这种情况下的方法足够好。

采用我的解决方案)这缩短并简化了代码

<?
function plan($a, $b, $c, $d)
{
    $t = date('H:i');
    if ($a <= $t && $t <= $b) { echo rand($c, $d); }
}
plan("12:09", "16:08", 260, 272);
plan("16:09", "18:08", 278, 286);
plan("18:09", "20:08", 293, 303);
plan("20:09", "23:38", 338, 359);
plan("23:39", "23:59", 293, 302);
plan("00:00", "01:05", 233, 241);
plan("01:06", "02:08", 195, 210);
plan("02:09", "02:23", 168, 179);
plan("02:24", "07:08", 121, 128);
plan("07:09", "09:08", 143, 160);
plan("09:09", "12:08", 187, 207);
?>

采用我的解决方案)这缩短并简化了代码

<?
function plan($a, $b, $c, $d)
{
    $t = date('H:i');
    if ($a <= $t && $t <= $b) { echo rand($c, $d); }
}
plan("12:09", "16:08", 260, 272);
plan("16:09", "18:08", 278, 286);
plan("18:09", "20:08", 293, 303);
plan("20:09", "23:38", 338, 359);
plan("23:39", "23:59", 293, 302);
plan("00:00", "01:05", 233, 241);
plan("01:06", "02:08", 195, 210);
plan("02:09", "02:23", 168, 179);
plan("02:24", "07:08", 121, 128);
plan("07:09", "09:08", 143, 160);
plan("09:09", "12:08", 187, 207);
?>

不要看更快的方式。。。看看正确的方法

  • 不要像比较字符串一样比较日期或时间
  • 使用
    mt_rand
    代替
    rand
  • 使用
    函数
    对象
    而不是长开关或if-else
例如:

$currTime = new DateTime();

$range = [
        new TimeRange("12:09-16:08", "260-272"),
        new TimeRange("16:09-18:08", "278-286"),
        new TimeRange("18:09-20:08", "293-303"),
        new TimeRange("20:09-23:38", "338-359"),
        new TimeRange("23:39-23:59", "195-210"),
        new TimeRange("01:06-02:23", "168-179"),
        new TimeRange("02:24-07:08", "121-128"),
        new TimeRange("07:09-09:08", "143-160"),
        new TimeRange("09:09-12:08", "187-241")
];

foreach($range as $timeRange) {
    if ($timeRange->inRange($currTime)) {
        printf("Current Time\t: %s\n", $currTime->format("H:i"));
        printf("Range Time\t: %s\n", $timeRange->getRange());
        printf("Random Value\t: %s\n", $timeRange->getRandom());
        break;
    }
}
输出

Current Time    : 01:53
Range Time      : 01:06 - 02:23
Random Value    : 168
二手货

class TimeRange {
    private $timeFrom, $timeTo;
    private $numFrom, $numTo;
    private $format;

    function __construct($time, $number, $format = "H:i") {
        list($timeFrom, $timeTo) = explode("-", $time);
        list($this->numFrom, $this->numTo) = explode("-", $number);
        $this->timeFrom = DateTime::createFromFormat($format, $timeFrom);
        $this->timeTo = DateTime::createFromFormat($format, $timeTo);
        $this->format = $format;
    }

    function inRange(DateTime $currTime) {
        return $currTime >= $this->timeFrom && $currTime <= $this->timeTo;
    }

    function getRandom() {
        return mt_rand($this->numFrom, $this->numTo);
    }

    function getRange() {
        return sprintf("%s - %s", $this->timeFrom->format($this->format), $this->timeTo->format($this->format));
    }
}
类时间范围{
私有$timeFrom,$timeTo;
私人$numFrom,$numTo;
私人$格式;
函数构造($time,$number,$format=“H:i”){
列表($timeFrom,$timeTo)=分解(“-”,$time);
列表($this->numFrom,$this->numTo)=分解(“-”,$number);
$this->timeFrom=DateTime::createFromFormat($format,$timeFrom);
$this->timeTo=DateTime::createFromFormat($format,$timeTo);
$this->format=$format;
}
范围内的函数(日期时间$currTime){
返回$currTime>=$this->timeFrom&&$currTime-timeTo;
}
函数getRandom(){
返回mt_rand($this->numFrom,$this->numTo);
}
函数getRange(){
返回sprintf(“%s-%s”,$this->timeFrom->format($this->format),$this->timeTo->format($this->format));
}
}

不要看更快的方式。。。看看正确的方法

  • 不要像比较字符串一样比较日期或时间
  • 使用
    mt_rand
    代替
    rand
  • 使用
    函数
    对象
    而不是长开关或if-else
例如:

$currTime = new DateTime();

$range = [
        new TimeRange("12:09-16:08", "260-272"),
        new TimeRange("16:09-18:08", "278-286"),
        new TimeRange("18:09-20:08", "293-303"),
        new TimeRange("20:09-23:38", "338-359"),
        new TimeRange("23:39-23:59", "195-210"),
        new TimeRange("01:06-02:23", "168-179"),
        new TimeRange("02:24-07:08", "121-128"),
        new TimeRange("07:09-09:08", "143-160"),
        new TimeRange("09:09-12:08", "187-241")
];

foreach($range as $timeRange) {
    if ($timeRange->inRange($currTime)) {
        printf("Current Time\t: %s\n", $currTime->format("H:i"));
        printf("Range Time\t: %s\n", $timeRange->getRange());
        printf("Random Value\t: %s\n", $timeRange->getRandom());
        break;
    }
}
输出

Current Time    : 01:53
Range Time      : 01:06 - 02:23
Random Value    : 168
二手货

class TimeRange {
    private $timeFrom, $timeTo;
    private $numFrom, $numTo;
    private $format;

    function __construct($time, $number, $format = "H:i") {
        list($timeFrom, $timeTo) = explode("-", $time);
        list($this->numFrom, $this->numTo) = explode("-", $number);
        $this->timeFrom = DateTime::createFromFormat($format, $timeFrom);
        $this->timeTo = DateTime::createFromFormat($format, $timeTo);
        $this->format = $format;
    }

    function inRange(DateTime $currTime) {
        return $currTime >= $this->timeFrom && $currTime <= $this->timeTo;
    }

    function getRandom() {
        return mt_rand($this->numFrom, $this->numTo);
    }

    function getRange() {
        return sprintf("%s - %s", $this->timeFrom->format($this->format), $this->timeTo->format($this->format));
    }
}
类时间范围{
私有$timeFrom,$timeTo;
私人$numFrom,$numTo;
私人$格式;
函数构造($time,$number,$format=“H:i”){
列表($timeFrom,$timeTo)=分解(“-”,$time);
列表($this->numFrom,$this->numTo)=分解(“-”,$number);
$this->timeFrom=DateTime::createFromFormat($format,$timeFrom);
$this->timeTo=DateTime::createFromFormat($format,$timeTo);
$this->format=$format;
}
范围内的函数(日期时间$currTime){
返回$currTime>=$this->timeFrom&&$currTime-timeTo;
}
函数getRandom(){
返回mt_rand($this->numFrom,$this->numTo);
}
函数getRange(){
返回sprintf(“%s-%s”,$this->timeFrom->format($this->format),$this->timeTo->format($this->format));
}
}
检查

控制结构
switch/case/default
VS.
if/elseif/else

Control Structures               | Total time
---------------------------------|-----------
if and elseif (using ==)         | 190 µs
if, elseif and else (using ==)   | 189 µs
if and elseif (using ===)        | 158 µs
if, elseif and else (using ===)  | 170 µs
switch / case                    | 200 µs
switch / case / default`         | 228 µs
结论:

使用
开关/case
if/elseif
几乎相同注意测试是取消的
==
(正好等于),并且比使用
=
(等于)稍微快一些。

检查

控制结构
switch/case/default
VS.
if/elseif/else

Control Structures               | Total time
---------------------------------|-----------
if and elseif (using ==)         | 190 µs
if, elseif and else (using ==)   | 189 µs
if and elseif (using ===)        | 158 µs
if, elseif and else (using ===)  | 170 µs
switch / case                    | 200 µs
switch / case / default`         | 228 µs
结论:

使用
开关/case
if/elseif
几乎相同注意测试是unsing
==
(正好等于),并且比使用
=
(等于)稍微快一些。

$currTime=date('H:i');
$arr=数组
(
"12:09,16:08"=>'260,272',
"16:09,18:08"=>'278, 286',
"18:09,20:08"=>'293, 303',
"20:09,23:38"=>'338, 359',
"23:39,23:59"=>'293, 302',
"01:06,02:08"=>'195, 210',
"00:00,00:30" =>'1,10'
//等等,等等。。。
);
foreach($arr作为$key=>$value){
$key=分解(“,”,$key);
$value=分解(“,”,$value);
如果($currTime>=$key[0]&&&$currTime
$currTime=date('H:i');
$arr=数组
(
"12:09,16:08"=>'260,272',
"16:09,18:08"=>'278, 286',
"18:09,20:08"=>'293, 303',
"20:09,23:38"=>'338, 359',
"23:39,23:59"=>'293, 302',
"01:06,02:08"=>'195, 210',
"00:00,00:30" =>'1,10'
//等等,等等。。。
);
foreach($arr作为$key=>$value){
$key=分解(“,”,$key);
$value=分解(“,”,$value);

如果($currTime>=$key[0]&&&$currTime
开关()
也许吧,但在这几个条件下,性能增益可以忽略不计。@Revent开关没有范围。我想说的一点是,每次检查都不需要第一个条件;只需在顶部添加一个新条件,时间不到
12:09
。我想不出更优雅的方式。预转换到int,但php速度很慢,所以我不会太在意。你甚至可以用条件($a<$b&&$b数组(mintime,maxtime,minrand,maxrand)创建一个数组
条目,循环通过它,
如果(mintime=time){echo rand(minrand,maxrand);break}
那么你以后只需要摆弄数组,而不是改变逻辑。顺便说一句:你当然可以
切换(true){case$currTime>=“12:09”&&$currTime@jrade我想Baba指的是
切换(true)
并将条件放在case语句中。这是我所知道的switch语句最严重的滥用,一个