In PHP, gettype() returns the type of a variable as a string, such as "integer", "string" or "array". It is a quick way to see what kind of data you are working with.
The names are a little old-fashioned. A float is reported as "double", a boolean as "boolean", and null as "NULL". For names that match type declarations (int, float, bool), use get_debug_type() in PHP 8, and for yes-or-no checks use the is_*() functions.
Syntax
Call gettype() with the values you need. Start with this line.
PHP
$type = gettype($value);
Call
What it does
gettype(42)
Returns "integer".
gettype(3.14)
Returns "double", the old name for a float.
gettype(null)
Returns "NULL" in uppercase.
gettype() can return: boolean, integer, double, string, array, object, resource, resource (closed), NULL and unknown type. Do not use it to test types in conditions, because the is_*() functions are faster and avoid spelling mistakes.
Library declarations
PHP
gettype(mixed $value): string
Parameters
Parameter
Description
value
Required. The variable or value whose type you want to read.
Return Value
Return Value
Description
string
One of "boolean", "integer", "double", "string", "array", "object", "resource", "resource (closed)", "NULL" or "unknown type".
PHP Examples
Example 1 — Types of Everyday Values
Ask gettype() about the common value types and print each answer.
"42" has quotes, so it is a string, even though it looks like a number.
Adding 0 forces PHP to read the string as a number. "42" becomes an integer, but "4.2" becomes a double.
10 / 2 divides evenly, so PHP returns the integer 5.
10 / 4 does not divide evenly, so the result 2. 5 is a double.
Example 3 — Describing Values with a Switch
Use the string from gettype() to give a friendly label to each value.
PHP
<?php
function describe($value) {
switch (gettype($value)) {
case "integer": return "whole number";
case "double": return "decimal number";
case "string": return "text";
case "boolean": return "true/false";
case "array": return "list";
case "NULL": return "nothing";
default: return "something else";
}
}
foreach ([7, 2.5, "hi", false, [1], null, new stdClass()] as $value) {
echo gettype($value), " -> ", describe($value), PHP_EOL;
}
Output
integer -> whole number
double -> decimal number
string -> text
boolean -> true/false
array -> list
NULL -> nothing
object -> something else
How It Works
The describe() function switches on the text that gettype() returns.
Each case must match the exact name, so you must write "double" and "NULL", not "float" and "null".
The loop prints the type name and the friendly label for seven different values.
An object such as stdClass is not matched by any case, so it falls to the default label.
Frequently Asked Questions
gettype() returns the type of a variable as a string, for example "integer", "string", "array" or "NULL".
It is a historical name that PHP keeps for backward compatibility. A double is simply a float. If you want the name float, use get_debug_type() in PHP 8.
gettype() returns old names (integer, double, boolean, NULL) and only says object for objects. get_debug_type() returns names like int, float, bool and null, and gives the class name for objects.
No. Comparing strings is easy to get wrong. Use is_int(), is_float(), is_string(), is_bool(), is_array() or is_null() instead, because they return a clean true or false.
gettype() returns only the type name as a string you can store or print. var_dump() prints the type and the value together and is better for quick debugging.