PHP: a short cut for isset and !empty?

后端 未结 11 2088
余生分开走
余生分开走 2021-02-09 13:45

I wonder if there any better ideas to solve the problem below,

I have a form with a number of input fields, such as,



        
11条回答
  •  情歌与酒
    2021-02-09 14:21

    User-defined function, I 'm afraid. But they come out short enough. I have one lying around somewhere if you want to take a look, but it's really trivial as you can imagine.

    Update:

    Here's one I found:

    define('PARAM_INT', 0);
    define('PARAM_STR', 1);
    
    function get_param($name, $default = null, $type = PARAM_INT) {
        $value = $default;
    
        if (isset($_POST[$name])) {
            $value = $_POST[$name];
        }
        else if (isset($_GET[$name])) {
            $value = $_GET[$name];
        }
    
        switch($type) {
            case PARAM_INT:
                $value = (int)$value;
                break;
            case PARAM_STR:
                break;
            default:
                // error your heart out here
        }
        return $value;
    }
    

    Of course now all the cool kids do it with filter_var, but the idea is the same.

提交回复
热议问题