In the context of PHP, what is the difference between using intval() and escaping functions for handling user input, and when should each method be employed?

When handling user input in PHP, it is important to sanitize the data to prevent security vulnerabilities such as SQL injection attacks. The difference between using intval() and escaping functions lies in their purpose. intval() is used to convert a variable to an integer, while escaping functions like mysqli_real_escape_string() are used to escape special characters in a string to make it safe for use in SQL queries. intval() should be used when expecting numerical input, while escaping functions should be used for other types of input.

// Using intval() for numerical input
$userInput = intval($_POST['number']);

// Using escaping function for non-numerical input
$userInput = mysqli_real_escape_string($conn, $_POST['text']);