PHP 8.3.4 Released!

gettype

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

gettypeLiefert den Datentyp einer Variablen

Beschreibung

gettype(mixed $value): string

Liefert den Datentyp der Variablen value. Zur Typprüfung sollten die is_* Funktionen verwendet werden.

Parameter-Liste

value

Die Variable, deren Typ ermittelt werden soll.

Rückgabewerte

Mögliche Werte der zurückgegebenen Zeichenkette sind:

  • "boolean"
  • "integer"
  • "double" (aus historischen Gründen wird "double" im Fall eines float zurückgegeben, und nicht einfach float.
  • "string"
  • "array"
  • "object"
  • "resource"
  • "resource (closed)" von PHP 7.2.0 an.
  • "NULL"
  • "unknown type"

Changelog

Version Beschreibung
7.2.0 Geschlossene Ressourcen werden nun als 'resource (closed)' gemeldet. Zuvor war der Rückgabewert für geschlossene Ressourcen 'unknown type'.

Beispiele

Beispiel #1 gettype()-Beispiel

<?php

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

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

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

integer
double
NULL
object
string

Siehe auch

  • get_debug_type() - Gets the type name of a variable in a way that is suitable for debugging
  • settype() - Legt den Typ einer Variablen fest
  • get_class() - Ermittelt den Klassennamen eines Objekts
  • is_array() - Prüft, ob die Variable ein Array ist
  • is_bool() - Prüft, ob eine Variable vom Typ boolean ist
  • is_callable() - Prüft, ob ein Wert als Funktion aus dem aktuellen Bereich aufgerufen werden kann.
  • is_float() - Prüft, ob eine Variable vom Typ float ist
  • is_int() - Prüft, ob eine Variable vom Typ int ist
  • is_null() - Prüft, ob eine Variable null enthält
  • is_numeric() - Prüft, ob eine Variable eine Zahl oder ein numerischer String ist
  • is_object() - Prüft, ob eine Variable vom Typ object ist
  • is_resource() - Prüft, ob eine Variable vom Typ resource ist
  • is_scalar() - Prüft, ob eine Variable skalar ist
  • is_string() - Prüft, ob Variable vom Typ string ist
  • function_exists() - Falls die angegebene Funktion definiert ist, wird true zurück gegeben
  • method_exists() - Prüft ob eine Methode innerhalb eines Objekts existiert

add a note

User Contributed Notes 2 notes

up
8
mohammad dot alavi1990 at gmail dot com
9 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
-49
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