PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] Yii random number generator function

 March 03, 2022     function, random, yii     No comments   

Issue

In yii i am creating random number generating function. I am using function as-

public function randomNumber()
{
    //for generating random number
    $length =3;
    $chars = array_merge(range(0,9));
    shuffle($chars);
    $password = implode(array_slice($chars, 0,$length));
    echo $password;

}

but in above function its creating random number as per length specified. i.e. if length is defined as 2, then it will create 2 digit number,if 3 then 3 digit number and so on. Now my condition is i want to generate random number between 1 to 1000. It can be 1 digit, 2 digit or 3 digit. So how to modify this function? Or is there any other way? Please help me...


Solution

Something like this?

<?php
$length = rand(1,3);
$chars = array_merge(range(0,9));
shuffle($chars);
$password = implode(array_slice($chars, 0,$length));
echo $password;
?>

But, actually, i think you must use it like this :)

<?php
$password = rand(1,1000);
echo $password;
?>


Answered By - Vladimir Gordienko
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing