Recommended: "PHP Video Tutorial"
Practical Function Collection
= 10) { $nx = 1 + ($ix % 10); $total += $nx; } else { $total += $ix; } } else { $total += $n; } $i++; } $total -= $last_n; $total *= 9; return $last_n == ($total % 10); } } if (!function_exists('blocking_lock')) { /** * 阻塞锁 * * @param string $lock_name 锁名字 * @param int $valid 有效秒数 * @return mixed */ function blocking_lock(string $lock_name, $valid = 3600) { $lock_key = 'blocking_lock'; while ($exp = Redis::hget($lock_key, $lock_name)) { if ($exp < microtime(true)) { Redis::hdel($lock_key, $lock_name); } usleep(10); } return Redis::hset($lock_key, $lock_name, microtime(true) + $valid); } } if (!function_exists('blocking_unlock')) { /** * 释放阻塞锁 * * @param string $lock_name * @return mixed */ function blocking_unlock(string $lock_name) { $lock_key = 'blocking_lock'; return Redis::hdel($lock_key, $lock_name); } } if (!function_exists('random_color')) { /** * 随机十六进制颜色 * * @return string */ function random_color() { $str = '#'; for ($i = 0; $i < 6; $i++) { $randNum = rand(0, 15); switch ($randNum) { case 10: $randNum = 'a'; break; case 11: $randNum = 'b'; break; case 12: $randNum = 'c'; break; case 13: $randNum = 'd'; break; case 14: $randNum = 'e'; break; case 15: $randNum = 'f'; break; } $str .= $randNum; } return $str; } } if (!function_exists('get_hour_history')) { /** * 获取当日历史小时 * * @return array */ function get_hour_history() { $history = []; for ($i = 0; $i <= date('H'); $i++) { $history[] = $i; } return $history; } }
The above is the detailed content of PHP utility function collection. For more information, please follow other related articles on the PHP Chinese website!