PHP gettype() Function

Quick summary: The PHP gettype() function returns the type of a variable as a string.

PHP gettype() Syntax

gettype(mixed $value): string
PHP

PHP gettype() Basic examples

var_dump(gettype(123));
PHP
Output:
string(7) "integer"

Returns the variable type.

var_dump(gettype("abc"));
PHP
Output:
string(6) "string"

Detects string types.

PHP gettype() Real-world usage

$value = 3.14;
echo gettype($value);
PHP
Output:
double

Used for debugging or logging.

PHP gettype() Edge cases

var_dump(gettype(null));
PHP
Output:
string(4) "NULL"

Null values return \"NULL\".

PHP gettype() Common mistakes

Using gettype for type checking

String comparisons are brittle.

Incorrect
gettype($v) === 'integer';
Correct
is_int($v);

Use is_* functions instead.

PHP gettype() Frequently Asked Questions

What does gettype() do?

Returns the type of a variable as a string.

Return type?

String.

Does it modify variable?

No.

Possible return values?

integer, string, array, object, boolean, NULL.

Use case?

Debugging.

Performance?

Fast.

Difference from is_*?

Returns string instead of boolean.

Handles null?

Yes.

Handles resources?

Yes.

Strict?

Yes.

Alternative?

get_debug_type().

Best practice?

Use is_* for checks.

PHP gettype() Related PHP Functions