What are the potential pitfalls of using intval() to filter integer values in PHP?

Using intval() to filter integer values in PHP can lead to unexpected results if the input is not strictly numeric. It can inadvertently convert non-numeric values to 0, which may not be the desired behavior. To solve this issue, it's recommended to use filter_var() with the FILTER_VALIDATE_INT filter, which will validate and return the integer value if it meets the criteria.

// Using filter_var() with FILTER_VALIDATE_INT to filter integer values
$input = "123abc";
$filtered_value = filter_var($input, FILTER_VALIDATE_INT);
if ($filtered_value !== false) {
    echo "Filtered integer value: " . $filtered_value;
} else {
    echo "Input is not a valid integer.";
}