What are the best practices for passing and handling values like 'id' in PHP form submissions?

When passing and handling values like 'id' in PHP form submissions, it is best practice to sanitize and validate the input to prevent SQL injection attacks and ensure data integrity. One way to do this is by using PHP's filter_input function to retrieve and sanitize the 'id' value from the form submission.

$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);

// Check if the 'id' value is valid
if ($id === false) {
    // Handle invalid input
    echo "Invalid input for id";
} else {
    // Process the 'id' value
    // Perform necessary actions with the sanitized 'id'
    echo "ID: " . $id;
}