Pop Quiz - Separate the serious coders from the cut-and-paste script kiddies.

NorHei

or maybe use :

include ("filename.php");


:lol:


Wo wir schon mal beim kürzen sind:

function randomString($len,$char) {
     for ($p = 0; $p < $len; $p++) $string .= $char[rand(0, strlen($char)-1)];
    return $string;
}


pcwacht

lol

Since I learned programming way back around 1980-ish then a good programmer use as less as and to make it as fast as possible.

Nowadays that has changed to make it reuasable and more readable.
Even creating loads of extra lines.

My solution would be somtehing like:

function randomString($len,$chars) {
   $rndMax = strlen($chars) - 1;
   $str = "";
   while ($len-- != 0) {
       $str .= $chars[rand(0, $rndMax)];
   }
   return $str;
}

$chars  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .
             "abcdefghijklmnopqrstuvwxyz" .
             "0123456789";
$str = randomString(8,$chars);
echo "$str\n";

Maybe adding some checks to set defaults.

Oh and if I neede this once I think I could do without the function as well ;)


John
[url="http://www.ictwacht.nl"]http://www.ictwacht.nl[/url] = Dutch ICT info
[url="http://www.pcwacht.nl"]http://www.pcwacht.nl[/url] = My first
both still work in progress, since years.....

NorHei

Here's a little test to separate the serious coders from the cut-and-paste script kiddies. Given the need to generate an arbitrarily long string consisting of random alpha-numeric characters, which solution is best?


http://zaemis.blogspot.de/2009/10/pop-quiz.html


Have fun  :lol: