What is the alternative code to if (isset ($_POST) && !empty ($_POST) to avoid warnings?

filter_input is the proper way to go. If it doesn’t return anything valid, it will return null:


$myvar = filter_input( INPUT_POST, 'something', FILTER_SANITIZE_STRING );

if ( empty( $myvar ) ) {
    // Do whatever you would have done for ! isset( $_POST['something'] )
}

// Use $myvar

filter_input won’t throw any notices if the requested index isn’t found, so it’s like having isset built-in to the function.

Edit: just be sure to use a FILTER_ to sanitize or validate, and note there are some gotchas that are documented in PHP’s documentation about these. For most general use-cases they should work fine, but always validate your user input appropriately once you have it (the same as you would when getting it directly from $_POST).