Ciao a tutti, oggi vedremo come controllare il tipo di una variabile in PHP.

Non perdiamo tempo in chiacchiere e vediamo subito le funzioni che abbiamo a nostra disposizione: (P.S. tutte queste funzioni sono di tipo boolean)

is_bool($var) il valore che avremo sarà true se $var è di tipo booleano, falso se non lo è.
is_int($var) il valore che avremo sarà true se $var è di tipo intero, falso se non lo è.
is_float($var) il valore che avremo sarà true se $var è di tipo decimale, falso se non lo è.
is_string($var) il valore che avremo sarà true se $var è di tipo stringa, falso se non lo è.
is_array($var) il valore che avremo sarà true se $var è di tipo array, falso se non lo è.
is_object($var) il valore che avremo sarà true se $var è un oggetto, falso se non lo è.

Altre funzioni interessanti sono:
is_numeric($var) il valore che avremo sarà true se $var è un numero o una stringa composta da soli numeri, falso se non lo è.
ctype_digit( $text ) il valore che avremo sarà true se $var è una stringa composta da soli caratteri numerici, falso se non lo è.

Ecco un esempio di queste due ultime funzioni:

$stringaNumerica = ‘2′;
$intero = 42;

ctype_digit($stringaNumerica);  // sarà vero
ctype_digit($intero);         // sarà falso

is_numeric($stringaNumerica);   // sarà vero
is_numeric($intero);          // sarà vero