Free cookie consent management tool by TermsFeed Generator PHP random number | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP random number

PHP random number

Last Updated on Feb 15, 2023

How?

Today we are going to talk about generating random numbers in PHP. Knowing how to generate a random number is more useful than you think. you can select random users and much more.

It’s very easy to generate random numbers in PHP.

 

All you need is this function

rand()

 

rand(min value, max value);

Let's see some examples:

echo rand(1,100);
// 46
echo rand(1,100);
// 73

Random Number with Decimals

Both min value and Max value must be int so what if you wanted to generate a random number that is not int.

 

Super easy!

Generate the random number and then divide it by 100

 

// Random number between 0 and 1
echo rand(1,100) / 100;
// 0.56

// Random number between 4 and 5
echo rand(4,500) / 100;
// 4.75

Conclusion

Now you know about random numbers in PHP.

I recommend you to open a PHP files and generate some random numbers. generate numbers with decimals as well.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • generate random numbers
  • generate random numbers with decimals

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Date and Time
Mar 21, 2023 programming

PHP Date and Time

Today we are going to learn about date and time in PHP. knowing how to work with dates and times and how to work with different formats of dates is very important. ...

14 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

7 Min Read Read More
PHP Dependency Management
Mar 22, 2023 programming

PHP Dependency Management

Today we are going to talk about dependency management in PHP. Your code might depends on some packages. You need a way to manage all the dependencies in PHP. ...

9 Min Read Read More
PHP Method Chaining
Mar 22, 2023 programming

PHP Method Chaining

Today we are going to talk about chaining methods in PHP. It’s cleaner and more readable and I don’t have to refer to my object every time I want to run one of the methods. ...

7 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...