How can PHP developers ensure that user input is properly sanitized and validated before being used in SQL queries?

PHP developers can ensure that user input is properly sanitized and validated before being used in SQL queries by using prepared statements and parameterized queries. This helps prevent SQL injection attacks by separating the SQL query logic from the user input data. Additionally, developers should use PHP functions like `filter_var()` to validate user input before using it in SQL queries.

// Example of using prepared statements to sanitize and validate user input in SQL queries
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');

// User input
$user_input = $_POST['user_input'];

// Prepare a SQL statement
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');

// Bind parameters
$stmt->bindParam(':username', $user_input, PDO::PARAM_STR);

// Execute the statement
$stmt->execute();

// Fetch results
$results = $stmt->fetchAll();