Generate a random integer with PHP
In this post, I am going to show how to generate a random integer with PHP.
To generate a random integer with PHP, there is a function called rand( ) which does the job.
Depends on whether you want the generation number is within a certain range or not, rand( ) takes in 2 parameters:
rand(minimum of the range inclusive, maximum of the range inclusive) the 2 parameters are integers. If you do not input any parameter into the function, the generation range will be between 0 and RAND_MAX inclusive.
Here are a few examples:
To have an random integer between 0 and RAND_MAX inclusive:
1 2 3 | <?php echo rand( ); ?> |
If you want to have a random integer between 1 and 50 inclusive, we do this:
1 2 3 | <?php echo rand(1,50); ?> |





Add A Comment