What is the best practice for accessing form fields in PHP?

When accessing form fields in PHP, it is best practice to sanitize and validate user input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One common way to do this is by using the $_POST superglobal array to access form field values submitted via POST method. It is also recommended to use functions like filter_input() or htmlspecialchars() to further sanitize the input.

// Accessing form fields submitted via POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '';
    $password = isset($_POST['password']) ? htmlspecialchars($_POST['password']) : '';
    
    // Validate and sanitize input further if needed
}