What are some best practices for handling dynamic values in URLs in PHP?

When handling dynamic values in URLs in PHP, it is important to properly sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One best practice is to use PHP's filter_input function to retrieve and sanitize input from the URL parameters.

// Retrieve and sanitize input from the URL parameters
$user_id = filter_input(INPUT_GET, 'user_id', FILTER_VALIDATE_INT);

// Check if the user_id is a valid integer
if ($user_id === false) {
    // Handle invalid input
    echo "Invalid user ID";
} else {
    // Use the sanitized user_id in your application logic
    echo "User ID: " . $user_id;
}