SitePoint Sponsor

User Tag List

View Poll Results: Which example is better?

Voters
2. You may not vote on this poll
  • First example

    2 100.00%
  • Second example

    0 0%
Results 1 to 7 of 7
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    which one is better?

    Which one do you prefer among the two?
    PHP Code:
    function genRandomNo($length 5){    
        
    $string substr(md5(uniqid(rand(), true)), 0$length);
        return 
    $string;

    Vs
    PHP Code:
    function genRandomNo($length 5){    
        
    $string substr(md5(microtime() * mktime()), 0$length);
        return 
    $string;


  2. #2
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I am scratching my head as to why you would put length = 5 in the parameter space? Did you just use it as an example?
    PHP Code:
    function genRandomNo(){     
        
    $string substr(md5(uniqid(rand(), true)), 05); 
        return 
    $string

    Would be cleaner for only doing 5 length. Or
    PHP Code:
    function genRandomNo($length){     
        
    $string substr(md5(uniqid(rand(), true)), 0$length); 
        return 
    $string

    would be better coding...
    Ryan B | My Blog | Twitter

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Of the two, the uniqid one is my choice. However, why would you want to generate a string of only hexadecimal characters (0-9a-f) and why assign the value to a variable only to return it?
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    I am scratching my head as to why you would put length = 5 in the parameter space?
    http://www.php.net/manual/en/functio...uments.default

  5. #5
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Interesting, thanks hash
    Ryan B | My Blog | Twitter

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Mildly peeved that the function name indicates it returns a number, yet, it doesn't.

    Yeah, I'm one of *those* geeks.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Some Sunday afternoon OOP fun. (untested)
    PHP Code:
    $generator = new CompositeStringGenerator();

    $generator->addGenerator(
        new 
    SpecialCharacterStringGenerator()
    )->
    addGenerator(
        new 
    NumericStringGenerator()
    )->
    addGenerator(
        new 
    LowerCaseAlphabeticStringGenerator()
    )->
    addGenerator(
        new 
    UpperCaseAlphabeticStringGenerator()
    );

    echo 
    $generator->generate(10); 
    PHP Code:
    interface StringGeneratorInterface
    {
        public function 
    generate($length);
    }

    abstract class 
    StringGenerator
    {
        protected
            
    $character_pool;
            
        public function 
    __construct($character_pool){
            
    $this->character_pool = (string)$character_pool;
        }
            
        public function 
    generate($length){
            if(
    === strlen($this->character_pool)){
                throw new 
    LogicException('Character pool is empty');
            }
            
    $string '';
            while(
    $length strlen($string)){
                
    $string .= substr(
                    
    str_shuffle(
                        
    $this->character_pool
                    
    ),
                    
    0,
                    
    1
                
    );
            }
            return 
    $string;
        }
    }

    class 
    CompositeStringGenerator implements StringGeneratorInterface
    {
        protected
            
    $generators = array();
            
        public function 
    addGenerator(StringGeneratorInterface $generator){
            
    array_push($this->generators$generator);
            return 
    $this;
        }
        
        public function 
    generate($length){
            
    $string '';
            while(
    $length strlen($string)){
                
    shuffle($this->generators);
                foreach(
    $this->generators as $generator){
                    if(
    $length <= strlen($string)){
                        break;
                    }
                    
    $string .= $generator->generate(1);
                }
            }
            return 
    str_shuffle($string);
        }
    }

    class 
    SpecialCharacterStringGenerator extends StringGenerator implements StringGeneratorInterface
    {
        public function 
    __construct(){
            
    parent::__construct('!"£$%^&*()_+=-{}][~#@;:?<>,.');
        }
    }

    class 
    NumericStringGenerator extends StringGenerator implements StringGeneratorInterface
    {
        public function 
    __construct(){
            
    parent::__construct('0123456789');
        }
    }

    class 
    LowerCaseAlphabeticStringGenerator extends StringGenerator implements StringGeneratorInterface
    {
        public function 
    __construct(){
            
    parent::__construct(implode(''range('a''z')));
        }
    }

    class 
    UpperCaseAlphabeticStringGenerator extends StringGenerator implements StringGeneratorInterface
    {
        public function 
    __construct(){
            
    parent::__construct(implode(''range('A''Z')));
        }

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •