Which one is better?

Which one do you prefer among the two?

function genRandomNo($length = 5){	
	$string = substr(md5(uniqid(rand(), true)), 0, $length);
	return $string;
}

Vs

function genRandomNo($length = 5){	
	$string = substr(md5(microtime() * mktime()), 0, $length);
	return $string;
}

http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default

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?


function genRandomNo(){     
    $string = substr(md5(uniqid(rand(), true)), 0, 5); 
    return $string; 
}

Would be cleaner for only doing 5 length. Or

function genRandomNo($length){     
    $string = substr(md5(uniqid(rand(), true)), 0, $length); 
    return $string; 
}

would be better coding…

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?

Interesting, thanks hash

Mildly peeved that the function name indicates it returns a number, yet, it doesn’t.

Yeah, I’m one of those geeks. :stuck_out_tongue:

Some Sunday afternoon OOP fun. (untested)


$generator = new CompositeStringGenerator();

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

echo $generator->generate(10);


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(0 === 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('!"&#163;$&#37;^&*()_+=-{}][~#@;:?<>,.');
    }
}

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')));
    }
}