PHP中的随机密码生成器未返回密码 [英] Random password generator in PHP not returning password

查看:112
本文介绍了PHP中的随机密码生成器未返回密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在php中设置随机密码生成器功能,但这对我不起作用.我收到错误消息:

I am trying to set up a random password generator function in php but this is not working for me. I get the error:

注意:第12行的C:\ xampp \ htdocs \ php-testing \ index.php中的数组到字符串的转换

Notice: Array to string conversion in C:\xampp\htdocs\php-testing\index.php on line 12

数组

我在做什么错了?

<?php
function pwGenerator($len = 10) {
    $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
    $pw = array();
    $length = strlen($charRange);
    for ($x = 0; $x < $len; $x++) {
        $n = rand(0, $length);
        $pw[] = $charRange[$n];
    }

    return $pw;
}

echo pwGenerator();

推荐答案

您不能直接将数组转换为字符串.因此,可以将字符附加在字符串中,而不是将其存储在数组中.现在它正在工作.

You cannot convert array into string directly. So instead of storing it in array you can attach characters with a string. and now it is working.

<?php
    function pwGenerator($len = 10) {
        $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
        $pw = null;
        $length = strlen($charRange);
        for ($x = 0; $x < $len; $x++) {
            $n = rand(0, $length);
            $pw .= $charRange[$n];
        }
        return $pw;
    }
    echo pwGenerator();
    ?>

这篇关于PHP中的随机密码生成器未返回密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆