Content · Programming · PHP

PHP gettype() Function: Usage and Examples

Understand PHP gettype() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use gettype() when you need to returns the PHP type name of a value as a string in a PHP application. The examples below show the actual operation and its limitations.

What Is gettype() in PHP?

Returns the PHP type name of a value as a string. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gettype(mixed $value): string

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

echo gettype(123);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

echo gettype([1,2]);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$values = [null, true, 3.5, "text"]; foreach ($values as $value) echo gettype($value) . PHP_EOL;

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

Type labels may be less specific than modern get_debug_type().

Comparison With Related Functions

ApproachDifference
gettype()Returns the PHP type name of a value as a string.
Related operationget_debug_type() gives detailed class names; gettype() reports broad PHP types.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationType labels may be less specific than modern get_debug_type().
Confusing this function with a related operationget_debug_type() gives detailed class names; gettype() reports broad PHP types.

Key Takeaways

  • Returns the PHP type name of a value as a string.
  • Type labels may be less specific than modern get_debug_type().
  • get_debug_type() gives detailed class names; gettype() reports broad PHP types.

Use gettype() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.