Several ways to generate random numbers in PHP
The following method is what I often use, it is more user-friendly
/** * 生成随机字符串 * @param int $length 要生成的随机字符串长度 * @param string $type 随机码类型:0,数字+大写字母;1,数字;2,小写字母;3,大写字母;4,特殊字符;-1,数字+大小写字母+特殊字符 * @return string */ function randCode($length = 5, $type = 0) { $arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|"); if ($type == 0) { array_pop($arr); $string = implode("", $arr); } else if ($type == "-1") { $string = implode("", $arr); } else { $string = $arr[$type]; } $count = strlen($string) - 1; for ($i = 0; $i < $length; $i++) { $str[$i] = $string[rand(0, $count)]; $code .= $str[$i]; } return $code; }
Generate fixed-length random numbers:
function generate_code($length = 4) { return rand(pow(10,($length-1)), pow(10,$length)-1); }
The following has the same effect as the above, but the writing method is different.
function generate_code($length = 4) { $min = pow(10 , ($length - 1)); $max = pow(10, $length) - 1; return rand($min, $max); }
For example:
echo generate_code(5); //输出结果: 56482
Refresh again, so the numbers and order will change.
The above two codes are relatively simple, but they can only generate numbers. Those who pursue code simplicity can use
$arr=range(1,10); shuffle($arr); foreach($arr as $values) { echo $values.","; } //输出结果:10,5,7,8,6,2,9,4,1,3,1
This is mainly generated by PHP's built-in function range(), which generates random numbers in two parameter ranges. And they can generate multiple sets of numbers, which is more rigid, and not all of them are placed in the method, but you can use them according to your needs.
The following one is not much different from the above one, but the generation method is different, mainly generated by the array_unique function.
$arr=array(); while(count($arr)<10) { $arr[]=rand(1,10); $arr=array_unique($arr); } echo implode(" ",$arr);
I won’t go into details about other generation methods here. We just need to remember the commonly used and more efficient ones. During the development process, we can write our own PHP according to our needs. Random number function.

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
