W3docs

Checking if a variable is an integer in PHP

In PHP, you can use the is_int() function to check if a variable is an integer.

In PHP, you can use the is_int() function to check if a variable is an integer.

Example:

Example of using the is_int() function to check if a variable is an integer in PHP

<?php

$x = 5;
if (is_int($x)) {
  echo "x is an integer";
} else {
  echo "x is not an integer";
}

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

You can also use is_integer() which is an alias of is_int().

Example of using the is_integer() function to check if a variable is an integer in PHP

<?php

$x = 5;
if (is_integer($x)) {
  echo "x is an integer";
} else {
  echo "x is not an integer";
}