Home » , » Random number tutorial in php

Random number tutorial in php

There are many use of random number. In php, there are a function to create random number and it is rand();
ex:
<?php
echo rand();
?>
It will output a number and refresh the page it will output deferent number.

You can create many random number by using bellow code:

<?php
echo rand(). "\n";
echo rand(). "\n";
echo rand(). "\n";
echo rand(). "\n";
?>

It will output deferent 4 random number.

If we want to get a number between a range then what can we do? Example, if we want get a number between 15 and 30 then we can use this code
<?php
echo rand(15, 30);
?>

Now We can use this in our project by adding some condition.
Ex:

<?php
$rand = rand(1, 3);
if ($rand == 1){
echo "Generated number is 1";
}else if ($rand == 2){
echo "Generated number is 2";
}else {
echo 'Generated number is 3';
}
?>

Good by and wish you EID MUBARAK.

0 comments:

Post a Comment

Popular Posts

Post Category

Blog Statistics

Now online

Page views
Powered by Blogger.