When using PHP for database operations in daily life, we often need to type verification of query results, especially in scenarios where precise numerical logic or type judgment is required. Although PHP is a weakly typed language, this does not mean that we can ignore the control of data types. This article will focus on how to use the is_integer() function to verify whether the data extracted from the database query results is an integer type, and explore its common pitfalls and correct usage.
is_integer() is a built-in type detection function in PHP, which is used to determine whether a variable is an integer type ( int ). The syntax is very simple:
is_integer(mixed $value): bool
Return true when the passed variable is an integer, otherwise false . This function is exactly equivalent to is_int() , which is alias for each other.
$number = 10;
if (is_integer($number)) {
echo "This is an integer";
} else {
echo "This is not an integer";
}
The output will be:
This is an integer
Even if a field is defined as INT type in a database (such as MySQL), PHP will return the result as a string by default when executing a query through PDO or mysqli. This means that even if the value in the database is an integer, the value obtained in PHP may be of a string type.
For example:
// Assumptions users In the table id The field is INT type
$stmt = $pdo->query("SELECT id FROM users WHERE id = 1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
var_dump($row['id']);
var_dump(is_integer($row['id']));
The output may be:
string(1) "1"
bool(false)
This shows that although the id in the database is an integer, PHP obtains the string "1" and is_integer() determines that the result is false .
In order for is_integer() to make the correct judgment, we need to perform type conversion when fetching the data or after.
$id = (int)$row['id'];
if (is_integer($id)) {
echo "id is an integer";
}
This method is simple and intuitive, and is suitable for use in scenarios with small or controllable data volume.
PDO supports returning different data types when querying. You can use PDO ::FETCH_NUM in combination with PDO::ATTR_EMULATE_PREPARES => false to try to make PDO automatically convert types, but this is still not as reliable as manual casting in actual use.
The following is an example based on actual application scenarios to determine whether the ID entered by the user matches the ID in the database and is all integers:
// User input
$userInput = $_GET['id'] ?? null;
if (!is_numeric($userInput)) {
exit("illegal ID");
}
// Query the database
$stmt = $pdo->prepare("SELECT id FROM users WHERE id = ?");
$stmt->execute([$userInput]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row && is_integer((int)$row['id'])) {
echo "This integer exists in the database ID";
} else {
echo "No corresponding found ID 或type错误";
}
In some interface designs, we may need to get parameters from the URL and determine whether they are integers. For example, users access it through the following URL:
https://gitbox.net/get-user.php?id=42
When processing such requests, you can also verify the parameters in combination with is_integer() :
$id = $_GET['id'] ?? null;
if (is_numeric($id) && is_integer((int)$id)) {
$id = (int)$id;
// Next, perform database operations
} else {
echo "illegal参数";
}
is_integer() is a very useful tool when verifying data types, especially in business logic that needs to ensure that the data is pure integers. However, since PHP returns numeric fields as strings from the database by default, it is usually necessary to perform a manual type conversion before use. Understanding this mechanism and correctly using is_integer() in combination with actual business scenarios can effectively improve the robustness of the program and data accuracy.