zoukankan      html  css  js  c++  java
  • php封装参数检查

    2021年5月1日09:30:23

    if (!function_exists('parameterCheck')) {
    
        /**
         * 如果是double的话,也使用float,需要处理其他类型数据自己添加
         * @param type $param 需要处理的参数
         * @param type $ExpectDataType  期望返回数据类型
         * @param type $defaultValue  如果没有值返回的默认值
         * @return type
         * @throws Exception
         */
        function parameterCheck($param, $ExpectDataType, $defaultValue) {
            $dataType = ['int', 'float', 'string', 'array'];
            if (!in_array($ExpectDataType, $dataType)) {
                throw new Exception('数据类型不存在');
            }
            if (empty($param)) {
                return $defaultValue;
            }
            if ($ExpectDataType == 'int') {
                return (int) htmlFilter($param);
            } elseif ($ExpectDataType == 'float') {
                return (float) htmlFilter($param);
            } elseif ($ExpectDataType == 'string') {
                return (string) htmlFilter($param);
            } elseif ($ExpectDataType == 'array') {
                return (array) $param;
            }
        }
    
    }
    
    if (!function_exists('htmlFilter')) {
    
        /**
         * 
         * @param type $param 需要html转义和去除空格的参数
         * @throws Exception
         */
        function htmlFilter($param) {
            return htmlspecialchars(trim($param), ENT_QUOTES, "UTF-8");
        }
    
    }
    QQ群 247823727 博客文件如果不能下载请进群下载
    如果公司项目有技术瓶颈问题,如有需要,请联系我,提供技术服务 QQ: 903464207
  • 相关阅读:
    百度搜索技巧
    phpstorm知识点
    A-Z
    边框
    display
    布局
    盒模型
    浮动
    字体与图标
    pselect 问题
  • 原文地址:https://www.cnblogs.com/zx-admin/p/14723832.html
Copyright © 2011-2022 走看看