这篇文章主要介绍了PHP生成指定长度随机数最简洁的方法,一行代码搞定随机数生成,是不是最简洁、最优雅的方法,需要的朋友可以参考下
刚才在写短信验证码模块,需要用到指定位数的随机数,,然后网上一找发现太可怕了这么简单的事情竟然用了好几十行多个循环嵌套……看来没有好脑仁儿真的不适合当程序员。
自写了一行版本:
function generate_code($length = 4) { return rand(pow(10,($length-1)), pow(10,$length)-1); }
为了便于理解,同时也为了这篇水文可以凑点字数,这是多行版:
function generate_code($length = 4) { $min = pow(10 , ($length - 1)); $max = pow(10, $length) - 1; return rand($min, $max); }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor