Generate random number from 0 – 9 in PHP?



To generate random number in PHP, use rand(). Let’s say the following is our input −

$storedValue ='0123456789';

And we want to display a random value from the above values.

Example

 Live Demo

<!DOCTYPE html> <html> <body> <?php    $storedValue ='0123456789';    $aRandomValue= $storedValue[rand(0,strlen($storedValue) - 1)];    echo "The random value=",$aRandomValue; ?> </body> </html>

Output

This will produce the following output

The random value=3
Updated on: 2020-10-13T06:59:58+05:30

535 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements