PHP 8.3.7 Released!

gettype

(PHP 4, PHP 5, PHP 7, PHP 8)

gettypeResituisce il tipo di una variabile

Descrizione

gettype(mixed $var): string

Resituisce il tipo della variabile PHP var. Per controllare il tipo, utilizzare le funzioni is_*.

Elenco dei parametri

var

La variabile di cui controllare il tipo.

Valori restituiti

I valori possibili per la stringa restituita sono:

Esempi

Example #1 Esempio di gettype()

<?php

$data
= array(1, 1., NULL, new stdClass, 'foo');

foreach (
$data as $value) {
echo
gettype($value), "\n";
}

?>

Il precedente esempio visualizzerà qualcosa simile a:

integer
double
NULL
object
string

Vedere anche:

  • settype() - Definisce il tipo di una variabile
  • get_class() - Restituisce il nome della classe di un oggetto
  • is_array() - Verifica se una variabile è un array
  • is_bool() - Verifica se una variabile è di tipo boolean
  • is_callable() - Verifica se il contenuto di una variabile può essere eseguito come una funzione
  • is_float() - Verifica se una variabile è di tipo float (decimale a virgola mobile)
  • is_int() - Verifica se una variabile è di tipo integer
  • is_null() - Verifica se la variabile è di tipo null
  • is_numeric() - Verifica se una variabile è un numero o una stringa numerica
  • is_object() - Verifica se una variabile è un object
  • is_resource() - Verifica se una variabile è una risorsa
  • is_scalar() - Verifica se la variabile è di tipo scalare
  • is_string() - Verifica se il tipo di una variabile sia stringa
  • function_exists() - Return true if the given function has been defined
  • method_exists() - Verifica se il metodo della classe esiste

add a note

User Contributed Notes 3 notes

up
9
mohammad dot alavi1990 at gmail dot com
11 months ago
Be careful comparing ReflectionParameter::getType() and gettype() as they will not return the same results for a given type.

string - string // OK
int - integer // Type mismatch
bool - boolean // Type mismatch
array - array // OK
up
0
brainsmith dot maleficent+php at gmail dot com
20 hours ago
Since ReflectionParameter::getType() and gettype() do not return the same type name, you can leverage the strpos() function.

use DateTime;
use ReflectionClass;

$myVar = 45;
# get the type of your variable
$varType = gettype($myVar);
$cls = new ReflectionClass(DateTime::class);
$mth = $cls->getMethod('setDate');
$params = $mth->getParameters();
foreach($params as $param) {
    $paramTypes = $param instanceof ReflectionUnionType
    ? $param->getTypes()
    : [$param];
    foreach($paramTypes as $paramType) {
        if (strpos($paramType, $varType) >= 0) {
            echo "Parameter '". $paramType->getName() ."' has type '". $paramType->getType() ."' which matches the variable's type of '$varType'.\n";
        }
    }
}
up
-57
Anonymous
2 years ago
Same as for "boolean" below, happens with integers. gettype() return "integer" yet proper type hint is "int".

If your project is PHP8+ then you should consider using get_debug_type() instead which seems to return proper types that match used for type hints.
To Top