PHP: a short cut for isset and !empty?

后端 未结 11 2086
余生分开走
余生分开走 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:33

    You can use a simple function

    function post_value_or($key, $default = NULL) {
        return isset($_POST[$key]) && !empty($_POST[$key]) ? $_POST[$key] : $default;
    }
    

    Then use:

    $pg_title = post_value_or('pg_title');
    // OR with a default
    $pg_title = post_value_or('pg_title', 'No Title');
    

提交回复
热议问题