drngnh708353 2018-02-03 15:13
浏览 28
已采纳

将功能与不同的呼叫方法结合起

I have 2 functions

This one generates numbers only

//Generate a string of only numbers.
function GenerateNUM($length)
{
    $alphabet = '1234567890';
    $tip = array();
    $alphaLength = strlen($alphabet) - 1;
    for ($i = 0; $i < $length; $i ++) {
        $n = rand(0, $alphaLength);
        $tip[] = $alphabet[$n];
    }
    return implode($tip);
}

This one generates numbers and letters

//Generate a string of numbers and letters.
function GenerateAll($length)
{
    $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $pass = array();
    $alphaLength = strlen($alphabet) - 1;
    for ($i = 0; $i < $length; $i ++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass);
}

and I call it like this

$ValidationCode = GenerateNUM(12);

or this

$ValidationCode = GenerateAll(12);

It works great but my question is do I really need 2 blocks of code? is there a way to create one function and be able to decide if numbers and letters are called or just numbers? or am I over thinking it?

  • 写回答

1条回答 默认 最新

  • dongzao9044 2018-02-03 15:22
    关注

    Example of a combined singular function based off your code:

    function GenerateCode($length,$type='ALPHA')
    {
        $alphabet = (($type == 'ALPHA')?'ABCDEFGHIJKLMNOPQRSTUVWXYZ':'') .'1234567890';
        $code = array();
        $alphaLength = strlen($alphabet) - 1;
        for ($i = 0; $i < $length; $i ++) {
            $n = rand(0, $alphaLength);
            $code[] = $alphabet[$n];
        }
        return implode($code);
    }
    
    $ValidationCode = GenerateCode(12,'NUM');// just makes a numbers only code
    $ValidationCode = GenerateCode(12,'ALPHA');// makes an alphanumeric code
    

    But there is nothing wrong with multiple small utility functions though. The naming keeps them clean and obvious their intent. However if you have a LOT of duplicated code between a bunch of utility functions, then you can combine them with a second parameter and adjusting small pieces inside based on that param (like above).


    A slightly better example of generating a random string like your function (includes upper and lower case lettering):

    function GenerateCode($length,$type='ALPHA')
    {
        $string = '';
        for ($n=1; $n <= $length; $n++) {
            if ($type == 'NUM') {
                $string .= mt_rand(0,9);
            } else {
                $randnum = mt_rand(0,61);
                $string .= ( ($randnum < 10) ? chr($randnum+48) :  // number chr 48 - 57
                            (($randnum < 36) ? chr($randnum+55) :  // upperletter chr 65 - 90
                                               chr($randnum+61) ));// lowerletter chr 97 - 122
            }
        }
        return $string;
    }
    
    // example GenerateCode(12,'ALPHA') output: XNu1n833b2ox
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵