The solution below is what I have cooked up to check that a variable from $_GET is an integer. It seems a bit convoluted and I am sure that there must be a simpler solution. The error array is used to show the user messages.
// Create array to hold errors
$errors = array();
// Get ID from URL
$user_id = isset($_GET['id']) ? $_GET['id'] : '';
// Check for empty ID
if (empty($user_id)) {
array_push($errors, 'Empty user ID');
} else {
// Check if ID is numeric
if (!is_numeric($user_id)) {
array_push($errors, 'Invalid user ID');
} else {
// Get numerical value of string (int or float)
$num_user_id = $user_id + 0;
// Check that number is an int
if (!is_integer($num_user_id))
array_push($errors, 'Invalid user ID');
}
}
(int)$_GET['id']if(empty($_GET['id']) || !is_integer((int)$_GET['id']) { $errors[]='Invalid user ID';}filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT );