PHP: a short cut for isset and !empty?

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

    I do not have enough rep to comment. However, the suggestion that vladkras made to use:

    $some_var = $_POST['some_var'] ? $_POST['some_var'] : NULL;
    

    is not E_ALL compliant. You should be checking array keys before accessing them using either empty() or isset() as others have suggested. Especially for user input.

    Also, his second suggestion to use the MySQL function "NULLIF()" as in the following manner:

    NULLIF('".$_REQUEST['some_var']."', '')
    

    is even worse. Inserting unsanitized user input directly into a SQL query is a primary vector for a SQL injection attack.

提交回复
热议问题