Function within function - PHP

-2

I want to do a function that generates a string in the alphanumeric format (XXXXXX-XXXXXX) and did the following function to do so:

    private static function generateCode() {
    function generate() {
        $alphaNumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
        $code = '';
        for ($i = 0; $i < 6; $i++) {
            $code .= $alphaNumeric[ rand(0,strlen($alphaNumeric) - 1) ];
        }
        return $code; // ex: X0XX0X
    }

    return sprintf("%s-%s",generate(),generate());

}

But I can not use generate (), how do I get this function inside the other?

    
asked by anonymous 27.08.2018 / 22:41

1 answer

2

It was supposed to be working, if you say the error that occurs maybe you can point it where it was, anyway I do not really know why you did it, you could simply create another private method and would already solve it and call% p>

private static function generateCode()
{
    return sprintf("%s-%s", self::generate(), self::generate());
}

private static function generate()
{
    $alphaNumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

    $code = '';

    for ($i = 0; $i < 6; $i++) {
        $code .= $alphaNumeric[ rand(0,strlen($alphaNumeric) - 1) ];
    }

    return $code; // ex: X0XX0X
}
    
27.08.2018 / 22:58