It's beatiful xD thanks.
Printable View
It's beatiful xD thanks.
Also doing (bool)1/0 will make true/false iirc
Okay, integers are not "faster". Integers can have a positive or negative "2147483647" on 32-bit systems, and up to "9223372036854775807" on 64-bit systems. That's a lot of bytes.
Booleans are 0 or 1 on all systems.
Integers in PHP take much more computation as PHP has a lot of features for math- it supports scientific notation, and changes the results to the more user-friendly whenever possible. You can also do math on several different base-numbers as PHP integers. So integers are in no way "faster" than boolean.
When you use any type in a conditional statement, it is automagicly converted to boolean anyway. As PHP versions change, the way that "magic" works can also change, making this style of programming not only a waste of resources, but also some-what unreliable in later version releases.
Let me correct you. The reason you use integers instead of boolean is because you're either lazy or stupid.
Don't use a chainsaw to do what can be done with scissors.
Well I optimized(??) it a bit more using booleans
PHP Code:<?php
/*
$l :: the length of your password
$s :: (1 to include symbols) (0 for no symbols)
$n :: (1 to include numbers) (0 for no numbers)
$uc :: (1 to include no upper-case letters) (0 for no upper-case letters)
$lc :: (1 to include no lower-case letters) (0 for no lower-case letters)
*/
function GeneratePassword( $l, $s, $n, $uc, $lc) {
$d = array('', '', '', '', '', '');
foreach(range('A', 'z') as $i) {
if(mb_strlen($d[2]) != 26) $d[2] .= $i; //uppercase letters
else if(mb_strlen($d[0]) != 6) $d[0] .= $i; //symbols
else break;
}
foreach(range('0', '9') as $i) $d[1] .= $i; //numbers
foreach (range('!', '/') as $i) $d[0] .= $i; //more symbols
$d[3] .= strtolower($d[2]); //lowercase letters
$arg = func_get_args();
for ($i = 1; $i < 5; $i++) if ($arg[$i]) $d[4] .= $d[$i-1];
if ( !$s && !$n && !$uc && !$lc ) $d[4] = $d[0].$d[2].$d[3];
for($p = 1; $p <= $l; $p++) $d[5] .= substr($d[4], mt_rand($p, strlen($d[4])), 1);
return htmlentities($d[5]);
}
echo GeneratePassword(5, false, false, true, true);
?>
Fantastic, this kinda thing would be good for on a registration page, there could be an ajax litebox which pops up if you click "Generate password" and then allows you to use it. Just an idea ;)