php 获取随机字符串

php 获取随机字符串代码:

/**
*$length 获取字符串的长度
*$isup 是否含有大写字母
*$max  最大的字母
*/

if (! function_exists ( 'getRand' )) {
    
    function getRand($length = 10, $isup = false, $max = false) {
        if (is_int ( $max ) && $max > $length) {
            $length = mt_rand ( $length, $max );
        }
        $output = '';
        
        for($i = 0; $i < $length; $i ++) {
            if ($isup) {
                $which = mt_rand ( 0, 2 );
            } else {
                $which = mt_rand ( 0, 1 );
            }
            
            if ($which === 0) {
                $output .= mt_rand ( 0, 9 );
            } elseif ($which === 1) {
                $output .= chr ( mt_rand ( 97, 122 ) );
            } else {
                $output .= chr ( mt_rand ( 65, 90 ) );
            }
        }
        return $output;
    }
}