Table of Contents
PHP can also do big things. Random functions, big things in PHP. Random functions
Home Backend Development PHP Tutorial PHP can also do big things with random functions, PHP can do big things with random functions_PHP Tutorial

PHP can also do big things with random functions, PHP can do big things with random functions_PHP Tutorial

Jul 13, 2016 am 09:57 AM
php

PHP can also do big things. Random functions, big things in PHP. Random functions

Write in front

PHP can also do great things. This is the classic usage of PHP syntax features and related function libraries that I have summarized. It may not really be able to achieve the effect of making a big difference, but mastering these methods can be of some help in your work and study. , I hope everyone can brainstorm and make "PHP Can Do Great Things" more exciting! Please indicate the source when reprinting (3mc2.com)

2. Foreword

PHP is a common scripting language, mainly because it is easy to learn and quick to use. Almost 50% of web programs include PHP (incomplete statistics). PHP provides a wealth of functions and API interfaces for development, which allows us to use its powerful built-in functions and extensions very conveniently. This article is the second article in the series "PHP Can Do Big Things", which mainly summarizes the use of PHP in random number generation. knowledge of equipment.

3. PHP random function

PHP random functions mainly include rand, mt_rand, array_rand, as well as the randomly "arranged" (shuffled) functions shuffle and str_shuffle, which can generate uniqid with a unique ID.

1. rand generates random numbers:

<&#63;php
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = strlen($base);
$random = '';
for ($i=0; $i < 16; $i++) { 
 $random.=$base[rand(0,$count-1)];
}
echo $random;
&#63;>
Copy after login

The rand() function uses libc's random number generator to generate random numbers. It is generally slow and has uncertain factors. It is recommended to use the mt_rand function instead.

The getrandmax() function can return the largest random number that the rand function can generate (my system is 32767), so when setting the second parameter of the rand function, do not exceed the return value of getrandmax.

2. mt_rand generates random numbers:

<&#63;php
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = strlen($base);
$random = '';
for ($i=0; $i < 16; $i++) { 
 $random.=$base[mt_rand(0,$count-1)];
}
echo $random;
&#63;>
Copy after login

Many old libc random number generators have some uncertain and unknown characteristics and are slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of » Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc. It is strongly recommended to use the mt_rand function instead of rand during development.

If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and mt_getrandmax(). For example, if you want a random number between 3 and 20 (inclusive), use mt_rand(3, 20).

3. array_rand function

<&#63;php
$base = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
 
$random = '';
for ($i=0; $i < 16; $i++) { 
 $random.=$base[array_rand($base)];
}
echo $random;
&#63;> 
Copy after login

array_rand returns a random key value in the array, which is somewhat similar to the mt_rand() function. The rest is nothing special, just use it flexibly.

4. shuffle function

<&#63;php
$base = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9');
if(shuffle($base)){
 print_r($base);
}
&#63;> 
Copy after login

The shuffle function is to disrupt the sequence of an array, which is a bit random. It is placed in the random function here. The return value is a bool value, which is equivalent to directly referencing the original variable.

5. str_shuffle function

<&#63;php
$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
echo str_shuffle($base);
&#63;>
Copy after login

The function of str_shuffle here is similar to that of shuffle. The only difference is the return value. The original string of str_shuffle remains unchanged.

6. uniqid function

<&#63;php
echo uniqid();
//54f806528172f
&#63;>
Copy after login

uniqid can generate unique strings, and the scope of this application can be quite wide

4. Summary
Random functions are almost the most basic functions of every language, and PHP’s support for random functions is no exception. Here we introduce the basic usage of rand, mt_rand, array_rand, shuffle, str_shuffle, and uniqid functions. More can be combined with practical applications. Use it flexibly.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/984628.htmlTechArticlePHP can also do big things. Random functions, php big things. Random functions are written in front. PHP can also do big things. This is the PHP syntax I summarized. The classic usage of features and related function libraries may not be truly practical...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles