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

how to use git restore and completely ignore the changes: 3 scenarios (video)
Nov 27, 2022 programming

how to use git restore and completely ignore the changes: 3 scenarios (video)

we are going to talk about ways to ignore the edits we made in a git project and restore the changes by git restore ...

9 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 Sanitize Data
Feb 15, 2023 programming

PHP Sanitize Data

Today we are going to talk about sanitizing data in PHP. Sanitizing data is a very important step, especially when you are dealing with user data. ...

7 Min Read Read More
What to Do After Learning Programming
Mar 17, 2024 programming

What to Do After Learning Programming

Congratulations! You've learned programming and now have a solid foundation in coding. But what should you do next? ...

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