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.
You May Also Like:
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.
1 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> <h1>Generate Random Number By Sort Order</h1> <form method="post" action="phprandom.php"> <input name="random" type="submit" value="Generate" /> </form> <!-- PHP Start ( https://webdevtrick.com ) --> <?php if(isset($_POST['random'])){ echo '<div class="numbers">'; $min=1; $max=100; $arrayNumersx = array(); for ($i=1; $i <= 50 ; $i++){ $numero = rand($min,$max); if (in_array($numero,$arrayNumersx)) { $i--; }else{ $arrayNumersx[] = $numero; } } sort($arrayNumersx); $tamanhoArray = count($arrayNumersx); for($y = 0; $y < $tamanhoArray; $y++){ echo "<div>", ($arrayNumersx[$y]), "</div>" ; } echo '</div>'; } ?> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ for giving style to this program. & Put these lines of codes.
1 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 |
/* 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.
Hi Sir how to add page viwes conut
Sure, I will post in this topic soon.
Hello Shaan thank you for your awesome tutorials.
Woud you be able to show countdown timer for multiple events on user input eg. input event (1); input time in minnutes (128) m; then SUBMIT Button. then these events shld show below in small box(table) with event 1, pic, Now; event 2, pic, 5m; event 3 pic, 10m; —> 10 displays dynamic . when event 1 (now) is completed the next upcoming event shows – always display a row of 10 events continuously.
These events shld then show on another linked page in table format as : e1 ….etc.. full list of events colour coded