When you need to save multiple files in same directory, then you need to generate random number everytime. Generating random numbers in PHP can be done by many ways.
In this article, we will discuss built-in functions which can be used to generate random numbers in PHP.
The rand() function generates a random integer value.
rand($min, $max)
<?php
echo(rand()); // 14870
If you want to get values between two digits, include these parameters. Suppose you want random number between 100 and 1000, pass them in function arguments.
<?php
echo(rand(100, 1000)); // 458
Generate random number using the Mersenne Twister Random number generator. This is four times faster and more secure than rand() function.
mt_rand($min, $max)
<?php
echo mt_rand(); // 14314917
mt_rand() function also accepts two arguments, minimum and maximum integer value. It will return integer number between minimum and miximum number.
<?php
echo mt_rand(100, 1000); // 752
random_int() function generates cryptographically secured pseudo-random integer number.
random_int($min, $max)
<?php
echo(random_int(100, 1000)); // 459
echo(random_int(-1000, -100)); // -121
I hope it will help you.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Detect a Mobile Device in jQuery
Use the JS matchMedia() Method You ca...How to determine if variable is undefined or null in JavaScript
Use the equality operator (==) In Jav...How to Remove Duplicate Values from an Array in PHP
In this article, i will share with you h...How to Convert a Date from yyyy-mm-dd to dd-mm-yyyy Format in PHP
Use the strtotime() Function You can...How to remove first character from a string in jQuery
Use the JavaScript substring() method...