How can the intval() function be used to prevent SQL injections when dealing with integer values in SQL queries in PHP?

To prevent SQL injections when dealing with integer values in SQL queries in PHP, the intval() function can be used to ensure that the input is treated as an integer. This function will convert any non-integer value to 0, effectively sanitizing the input and preventing malicious SQL injection attempts.

// Example usage of intval() to prevent SQL injections with integer values
$userId = intval($_GET['user_id']);

// SQL query using the sanitized integer value
$sql = "SELECT * FROM users WHERE id = $userId";