Maison  >  Article  >  développement back-end  >  Comment vérifier de manière fiable si une variable représente un entier en PHP ?

Comment vérifier de manière fiable si une variable représente un entier en PHP ?

Susan Sarandon
Susan Sarandonoriginal
2024-10-19 13:32:01741parcourir

How to Reliably Check if a Variable Represents an Integer in PHP?

Vérifier si une variable est un entier en PHP

Lorsque vous travaillez avec la saisie de l'utilisateur, il est crucial de valider les données pour garantir leur intégrité. Une tâche de validation courante consiste à vérifier si une variable représente un entier. PHP fournit la fonction is_int() à cet effet, mais elle ne produit pas toujours les résultats souhaités.

Pourquoi is_int() pourrait ne pas fonctionner comme prévu

L'utilisation de is_int() peut renvoyer des faux positifs pour les valeurs qui semblent être des entiers mais qui ne sont pas reconnues comme telles par la fonction. Par exemple, considérons le cas où un utilisateur saisit « 1 » comme numéro de page :

<code class="php">$page = $_GET['p'];</code>

Dans ce cas, $page se verra attribuer la chaîne « 1 », ce qui entraînera is_int($page ) renvoyant false.

Méthodes de validation alternatives

Pour vérifier de manière fiable si une variable est un entier, envisagez d'utiliser des méthodes alternatives qui fournissent des résultats plus précis :

1. FILTER_VALIDATE_INT Way

Le filtre FILTER_VALIDATE_INT peut être utilisé pour valider des entrées entières :

<code class="php">if (filter_var($page, FILTER_VALIDATE_INT) === false) {
    // Not an integer
}</code>

Sortie :

TEST -1: -1 (type:integer) Your variable is an integer ✔
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

2. Méthode de comparaison de casting

La comparaison de la représentation sous forme de chaîne d'une variable avec sa représentation entière peut également révéler s'il s'agit d'un entier :

<code class="php">if (strval($page) !== strval(intval($page))) {
    // Not an integer
}</code>

Sortie :

TEST -1: -1 (type:integer) Your variable is an integer ✔
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

3. CTYPE_DIGIT Way

En utilisant la fonction ctype_digit, vous pouvez vérifier les nombres positifs et 0 :

<code class="php">if (!ctype_digit(strval($page))) {
    // Not an integer
}</code>

Sortie :

TEST -1: -1 (type:integer) # Your variable is not an integer ✘
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

4. REGEX Way

Les expressions régulières peuvent également être utilisées pour valider des entiers :

<code class="php">if (!preg_match('/^-?\d+$/', $page)) {
    // Not an integer
}</code>

Sortie :

TEST -1: -1 (type:integer) Your variable is an integer ✔
TEST 0 : 0 (type:integer) is an integer ✔
TEST 1 : 42 (type:integer) is an integer ✔
TEST 2 : 4.2 (type:double) is not an integer ✘
TEST 3 : 0.42 (type:double) is not an integer ✘
TEST 4 : 42 (type:double) is an integer ✔
TEST 5 : 42 (type:string) is an integer ✔
TEST 6 : a42 (type:string) is not an integer ✘
TEST 7 : 42a (type:string) is not an integer ✘
TEST 8 : 36 (type:integer) is an integer ✔
TEST 9 : 1337 (type:double) is an integer ✔

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn