note 26821 modified in control-structures.switch by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 13:54:43 +0000 |
| Subject: | note 26821 modified in control-structures.switch by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80061@lists.php.net to get a copy of this message | ||
this is a simple function that returns a random string and uses
switch to determine what kind of string you want.
function r( [string prefix][,int type,][,int chars] );
type = 1 -> only numbers
type = 2 -> only letters
type = 3 -> both
<?php
function r($prefixo = "",$tipo = 3,$numcaracteres = 10) {
switch ($tipo) {
case 1:
for ($x = 1; $x <= $numcaracteres; $x++) {
$rnum .= chr(rand(48,57));
}
return $prefixo . $rnum;
break;
case 2:
for ($x = 1; $x <= $numcaracteres; $x++) {
if (rand(1,2) == 1) { $rletras .= chr(rand(65,90)); }
else { $rletras .= chr(rand(97,122)); }
}
return $prefixo . $rletras;
break;
case 3:
for ($x = 1; $x <= $numcaracteres; $x++) {
$r = rand(1,3);
if ($r == 1) { $rstring .= chr(rand(65,90)); }
elseif ($r == 2) { $rstring .= chr(rand(97,122)); }
else { $rstring .= chr(rand(48,57)); }
}
return $prefixo . $rstring;
break;
}
}
?>
--was--
this is a simple function that returns a random string and uses
switch to determine what kind of string you want.
function r( [string prefix][,int type,][,int chars] );
type = 1 -> only numbers
type = 2 -> only letters
type = 3 -> both
function r($prefixo = "",$tipo = 3,$numcaracteres = 10) {
switch ($tipo) {
case 1:
for ($x = 1; $x <= $numcaracteres; $x++) {
$rnum .= chr(rand(48,57));
}
return $prefixo . $rnum;
break;
case 2:
for ($x = 1; $x <= $numcaracteres; $x++) {
if (rand(1,2) == 1) { $rletras .= chr(rand(65,90)); }
else { $rletras .= chr(rand(97,122)); }
}
return $prefixo . $rletras;
break;
case 3:
for ($x = 1; $x <= $numcaracteres; $x++) {
$r = rand(1,3);
if ($r == 1) { $rstring .= chr(rand(65,90)); }
elseif ($r == 2) { $rstring .= chr(rand(97,122)); }
else { $rstring .= chr(rand(48,57)); }
}
return $prefixo . $rstring;
break;
}
}
http://php.net/manual/en/control-structures.switch.php