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 Arrays
Mar 21, 2023 programming

PHP Arrays

Today we are going to learn about arrays in PHP. Array is a datatype that can hold more than one value so instead of having multiple variables you can hold all those values in one variable. ...

9 Min Read Read More
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 Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
PHP Array Diff
Feb 14, 2023 programming

PHP Array Diff

Today we are going to talk about array diff functions in PHP. If you want to find the items in your array that are not present in the other arrays, array diff is your friend. ...

5 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 ...