ext/crypt_code.php
/**
     * Generate Math calculation codes
     *
     * @return array
     */
Generate Math calculation codes
@return array
Generate Math calculation codes @return array
private static function gen_calc(): array
    {
        $result = [];
        $option = ['+', '-', '*'];
        $result['char'][] = mt_rand(0, 9);
        $result['char'][] = mt_rand(0, 2);
        $result['char'][] = mt_rand(0, 9);
        $result['char'][] = mt_rand(0, 2);
        $result['char'][] = mt_rand(0, 9);
        $calc = static function (int $num_1, int $opt, int $num_2): int
        {
            switch ($opt) {
                case 2:
                    $res = $num_1 * $num_2;
                    break;
                case 1:
                    $res = $num_1 - $num_2;
                    break;
                default:
                    $res = $num_1 + $num_2;
                    break;
            }
            unset($num_1, $opt, $num_2);
            return $res;
        };
        switch ($result['char'][1] <=> $result['char'][3]) {
            case -1:
                $result['code'] = (string)$calc($result['char'][0], $result['char'][1], $calc($result['char'][2], $result['char'][3], $result['char'][4]));
                break;
            default:
                $result['code'] = (string)$calc($calc($result['char'][0], $result['char'][1], $result['char'][2]), $result['char'][3], $result['char'][4]);
                break;
        }
        $result['char'][0] = (string)$result['char'][0];
        $result['char'][2] = (string)$result['char'][2];
        $result['char'][4] = (string)$result['char'][4];
        $result['char'][1] = $option[$result['char'][1]];
        $result['char'][3] = $option[$result['char'][3]];
        $result['char'][] = '=';
        unset($option, $calc);
        return $result;
    }
private static function gen_calc ( ) : array { $ result = [ ] ; $ option = [ '+' , '-' , '*' ] ; $ result [ 'char' ] [ ] = mt_rand ( 0 , 9 ) ; $ result [ 'char' ] [ ] = mt_rand ( 0 , 2 ) ; $ result [ 'char' ] [ ] = mt_rand ( 0 , 9 ) ; $ result [ 'char' ] [ ] = mt_rand ( 0 , 2 ) ; $ result [ 'char' ] [ ] = mt_rand ( 0 , 9 ) ; $ calc = static function ( int $ num_1 , int $ opt , int $ num_2 ) : int { switch ( $ opt ) { case 2 : $ res = $ num_1 * $ num_2 ; break ; case 1 : $ res = $ num_1 - $ num_2 ; break ; default : $ res = $ num_1 + $ num_2 ; break ; } unset ( $ num_1 , $ opt , $ num_2 ) ; return $ res ; } ; switch ( $ result [ 'char' ] [ 1 ] <=> $ result [ 'char' ] [ 3 ] ) { case - 1 : $ result [ 'code' ] = ( string ) $ calc ( $ result [ 'char' ] [ 0 ] , $ result [ 'char' ] [ 1 ] , $ calc ( $ result [ 'char' ] [ 2 ] , $ result [ 'char' ] [ 3 ] , $ result [ 'char' ] [ 4 ] ) ) ; break ; default : $ result [ 'code' ] = ( string ) $ calc ( $ calc ( $ result [ 'char' ] [ 0 ] , $ result [ 'char' ] [ 1 ] , $ result [ 'char' ] [ 2 ] ) , $ result [ 'char' ] [ 3 ] , $ result [ 'char' ] [ 4 ] ) ; break ; } $ result [ 'char' ] [ 0 ] = ( string ) $ result [ 'char' ] [ 0 ] ; $ result [ 'char' ] [ 2 ] = ( string ) $ result [ 'char' ] [ 2 ] ; $ result [ 'char' ] [ 4 ] = ( string ) $ result [ 'char' ] [ 4 ] ; $ result [ 'char' ] [ 1 ] = $ option [ $ result [ 'char' ] [ 1 ] ] ; $ result [ 'char' ] [ 3 ] = $ option [ $ result [ 'char' ] [ 3 ] ] ; $ result [ 'char' ] [ ] = '=' ; unset ( $ option , $ calc ) ; return $ result ; }
Generate Math calculation codes
 @return array
Generate Math calculation codes @return array
//Allowed calculate options //Generate random numbers and option indicators //Calculate function //Calculate result //Change number integer to string //Change option indicator to option string //Add suffix //Free memory