W3docs

gettype()

The gettype() function is a built-in function in PHP that returns the data type of a given variable. It can be used to check the data type of a variable and

Introduction

PHP is a dynamically typed language: a variable's type is decided at runtime, not declared up front. The built-in gettype() function lets you inspect that type at runtime, returning a human-readable string such as "integer" or "string". It is handy when you are debugging unexpected values, branching on the type of a mixed input, or writing log messages that describe what you actually received.

This page covers the function's syntax, the exact strings it can return, the legacy type-name quirks that trip people up, and when to reach for gettype() versus the dedicated is_* functions.

Syntax

gettype(mixed $value): string

gettype() takes a single argument, $value — the variable whose type you want to know — and returns a string naming that type. It never throws and never modifies its argument.

Return values

gettype() returns one of a fixed set of strings:

Returned stringPHP type
"boolean"bool (true / false)
"integer"int
"double"float (yes, double, not "float" — see the gotcha below)
"string"string
"array"array
"object"any object instance
"resource"an open resource (e.g. a file handle)
"resource (closed)"a closed resource (PHP 7.2+)
"NULL"the null value
"unknown type"a type PHP cannot otherwise name

Basic example

Example of PHP gettype()

php— editable, runs on the server

Here we define four variables of different types and ask gettype() to name each one. The output is string, integer, boolean, and array — one type name per line.

The "double" and "integer" gotcha

The strings gettype() returns do not match the keywords you use in type declarations. A float is reported as "double", and an int is reported as "integer":

<?php
echo gettype(3.14), "\n"; // double  (NOT "float")
echo gettype(7),    "\n"; // integer (NOT "int")
echo gettype(null), "\n"; // NULL    (uppercase)
?>

Because of this, comparing gettype() output against "float" or "int" silently fails. If you only need a yes/no answer about a single type, prefer the dedicated functions — is_int(), is_float(), is_string(), is_array(), and friends — which are both faster and free of the naming surprise:

<?php
$value = 7;

// Brittle: depends on the legacy name
if (gettype($value) === "integer") { /* ... */ }

// Clearer and faster:
if (is_int($value)) { /* ... */ }
?>

On PHP 8.0 and later, get_debug_type() is the modern alternative: it returns the canonical names (int, float, bool) and, for objects, the actual class name instead of just "object".

When to use gettype()

  • Debugging and logging — printing the type of a value alongside its content while tracking down a bug.
  • Generic handling of mixed input — branching in a switch when a function legitimately accepts several types.
  • Quick inspection — a one-off check at the console without caring about the modern naming.

For full structured dumps of a value (its type and its contents, recursively), use var_dump() or print_r(). To change a variable's type rather than read it, see settype(). For a broader tour of PHP's type system, read PHP Data Types.

Conclusion

gettype() gives you a quick, string-based answer to "what type is this value?" — useful for debugging, logging, and handling mixed input. Just remember the legacy names ("double" for floats, "integer" for ints) and reach for the is_* functions or get_debug_type() when you need a precise, future-proof check.

Practice

Practice
What is the purpose of the 'gettype()' function in PHP?
What is the purpose of the 'gettype()' function in PHP?
Was this page helpful?