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

git branch workflow: 7 easy steps (video)
Nov 27, 2022 programming

git branch workflow: 7 easy steps (video)

Transcription Hello This is amir Today we are going to talk about git branching workflow I will go through it step by step and help you understand the process ...

9 Min Read Read More
PHP range
Feb 15, 2023 programming

PHP range

Today we are going to talk about range function in PHP. It's a very simple yet very powerful tool and can save you a lot of time. it's also more readable than the alternatives. ...

3 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

6 Min Read Read More
Machine Learning in PHP
Mar 18, 2023 programming

Machine Learning in PHP

Machine learning is a way for computers to learn and improve at tasks without being specifically programmed to do so. Are you curious about machine learning and PHP? ...

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