PHP gettype() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. The variable or value whose type you want to read.

Return Value

Return ValueDescription
stringOne 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.

PHP
<?php
echo gettype(42), PHP_EOL;
echo gettype(3.14), PHP_EOL;
echo gettype("hi"), PHP_EOL;
echo gettype(true), PHP_EOL;
echo gettype([1, 2]), PHP_EOL;
echo gettype(null), PHP_EOL;

How It Works

  1. A whole number such as 42 is an integer.
  2. A number with a decimal point, such as 3. 14, is reported as double.
  3. Text is a string, and true or false is a boolean.
  4. A list is an array, and null is reported in uppercase as NULL.

Example 2 — Numbers Stored as Text

A number in quotes is a string, and arithmetic can change the type of a result.

PHP
<?php
echo gettype("42"), PHP_EOL;
echo gettype(42), PHP_EOL;
echo gettype("42" + 0), PHP_EOL;
echo gettype("4.2" + 0), PHP_EOL;
echo gettype(10 / 2), PHP_EOL;
echo gettype(10 / 4), PHP_EOL;

How It Works

  1. "42" has quotes, so it is a string, even though it looks like a number.
  2. Adding 0 forces PHP to read the string as a number. "42" becomes an integer, but "4.2" becomes a double.
  3. 10 / 2 divides evenly, so PHP returns the integer 5.
  4. 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;
}

How It Works

  1. The describe() function switches on the text that gettype() returns.
  2. Each case must match the exact name, so you must write "double" and "NULL", not "float" and "null".
  3. The loop prints the type name and the friendly label for seven different values.
  4. 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.

Next: intval()

intval() converts a value to an integer and returns it. Decimal parts are cut off, not rou.

intval() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful