How to Generate Random Numbers in PHP?

How to Generate Random Numbers in PHP?

Today I am sharing a program about create random numbers by sort order in PHP. There I am covering two topics. First, create random number in PHP. Second I am printing those random number in sort by order

I saw many guys in the different group who was asking for How To Generate Random Numbers In PHP? That’s why today I will cover this topic and give source code of a program in this topic. First of all, I want to say that I am not a PHP expert. I am just learning It. In the future, I will learn other backed languages like Node JS.

Today I am sharing a program about create random numbers by sort order in PHP. There I am covering two topics. First, create random number in PHP. Second I am printing those random number in sort by order. Now, if are you thinking this is an advanced program, Then I want to say you that this is a very small program.

This program is fully dynamic, You have to code one time and this will print 25 numbers using a loop. This program is also a little bit stylish. And at the same time, it looks stylish also. Because I used CSS but very less. Let’s take the preview of this program first.

Preview Of Random Numbers Generator Program In Sort By Order

First, Take a look at this PHP program. In other words, see how this program looks visually.


Now you can clearly see the function of this automatically generating number. You can change this program as you want after understanding the code.

Generate Random Numbers In PHP Programs Source Code

Before sharing source code, I want to say about this program. Basically, this program is created using the PHP rand(min,max); function. The rand() function generates a random integer ( know more about PHP rand ). Then I printed all numbers using the echo "" command. I also use a PHP loop for to print multi times.

You have to create 2 files for this program. One for PHP, & another for CSS. Just follow steps:

phprandom.php

Create a PHP file named ‘phprandom.php‘ and put these codes give here below. If you want to change this file name, Then you also have to change the file name in the forms action="" section.

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html> <head> <meta charset="utf-8"> <title>Generate Random Number | Webdevtrick.com</title> <link href="style.css" rel="stylesheet"> </head> <body>

    &lt;h1&gt;Generate Random Number By Sort Order&lt;/h1&gt;

 

    &lt;form method="post" action="phprandom.php"&gt;
    &lt;input name="random" type="submit" value="Generate" /&gt;
    &lt;/form&gt;
    &lt;!-- PHP Start ( https://webdevtrick.com ) --&gt;
    &lt;?php
    if(isset($_POST['random'])){ 
    echo '&lt;div class="numbers"&gt;';

&nbsp;&nbsp;&nbsp;&nbsp;$min=1;
&nbsp;&nbsp;&nbsp;&nbsp;$max=100;
    $arrayNumersx = array();
&nbsp;&nbsp;&nbsp;&nbsp;for ($i=1; $i &lt;= 50 ; $i++){
&nbsp;&nbsp;&nbsp;&nbsp;    
&nbsp;&nbsp;    &nbsp;&nbsp; $numero = rand($min,$max);
&nbsp;&nbsp;if (in_array($numero,$arrayNumersx)) {
&nbsp;&nbsp;        &nbsp;&nbsp;&nbsp;&nbsp;$i--; 
&nbsp;&nbsp;    &nbsp;&nbsp;&nbsp;&nbsp;}else{
&nbsp;&nbsp;    &nbsp;&nbsp;&nbsp;&nbsp;    
&nbsp;&nbsp;        &nbsp;&nbsp;&nbsp;&nbsp;$arrayNumersx[] = $numero;
&nbsp;&nbsp;    &nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
    sort($arrayNumersx);
&nbsp;&nbsp;
    $tamanhoArray = count($arrayNumersx);

    for($y = 0; $y &lt; $tamanhoArray; $y++){
&nbsp;&nbsp;    echo "&lt;div&gt;", ($arrayNumersx[$y]), "&lt;/div&gt;" ;
    }
    echo '&lt;/div&gt;';
    }
    ?&gt;
&lt;/body&gt;    

</html>

style.css

Now create a CSS file named ‘style.css‘ for giving style to this program. & Put these lines of codes.

/* Code By Webdevtrick ( https://webdevtrick.com ) */
body {
        background: #333;
        }
 
        h1{
        text-align: center;
        color: white;
        text-transform: uppercase;
        font-size: 3em;
        font-family: "helvetica";
        }
 
        .numbers{
        display: flex;
        flex-direction: row;
   flex-wrap: wrap;
   background: #333;
        }
        .numbers div {
        background: #2ab1ce;
        padding: 5px;
        margin: 2px;
        justify-content: space-around;
        color: #fff;
        font-size: 4em;
        width: 9%;
        text-align: center
        }
        form{
        display: flex;
        justify-content: center;
margin-bottom: 50px; }   form input{ background: #2ab1ce; color: #fff; font-size: 2em; }

That’s It. Now you have successfully created generate random numbers in PHP program with sort by order. If you have any doubt or question comment down below.

Thanks For Visiting, Keep Visiting. If you liked this post, share it with all of your programming buddies!

php css web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...