You are here: Home / Topics / Explain rand() function in PHP

Explain rand() function in PHP

Filed under: PHP on 2022-10-31 21:20:55

Hello guys, I am Pritvik Sharma. In this post, I am going to tell you what rand() function does in case of PHP. 

rand() function is inbuilt function in PHP which is used to generate a random integer. If you write rand() without its minimum and maximum value it will generate a random number from all possible integers. But if you want to generate a random number from given range, write this function with its minimum and maximum range.

For example

<?php 

echo rand();

 ?>

output of this program can be any integer number. like 65, 88, 2323 etc.

Use rand() function with minimum and maximum range

If you want to generate a random integer from the range given. See the below example

<?php

echo rand(100, 1000);  //this function will generate a random integer between 100 and 1000.

?>

Output of this function will be some what like 666, 123, 987 etc. 

Hope you got this function. You can learn and practice PHP mcqs here.

About Author:
P
Pritvik     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.