PHPType Checking Functions

Type Checking Functions

Because PHP variables can hold any type and that type can change at runtime, you frequently need to ask "what type is this, right now?" or "is this specifically an array?" before acting on a value. PHP provides two complementary families of tools for this: functions that report or change a variable's type (gettype() and settype()), and a family of is_*() functions that each answer a single yes/no question about a value's type.

gettype()

gettype() takes any value and returns a string naming its type. The strings it returns are historical names, not the same words you use when casting — worth memorizing once so the output does not surprise you.

gettype() on each scalar and compound type

PHP
<?php
echo gettype(42) . "\n";
echo gettype(3.14) . "\n";
echo gettype("hello") . "\n";
echo gettype(true) . "\n";
echo gettype([1, 2]) . "\n";
echo gettype(null) . "\n";
integer
double
string
boolean
array
NULL
gettype() names do not match cast/hint keywords
`gettype()` returns `"integer"` and `"double"`, but you cast with `(int)` and `(float)`, and you write type hints as `int` and `float`. This mismatch is a long-standing historical quirk of PHP. Because of it, `gettype()` is best used for human-readable debug output, not for comparing against a type name in application logic — use the `is_*()` functions for that instead.
settype()

settype() is the converse of gettype(): instead of reporting a type, it converts a variable in place to a target type and returns true on success. It takes the variable by reference as its first argument and a type name as a string for its second.

Converting a variable in place with settype()

PHP
<?php
$quantity = "12";
var_dump($quantity);

settype($quantity, "integer");
var_dump($quantity);

$flag = "yes";
settype($flag, "boolean");
var_dump($flag);
string(2) "12"
int(12)
bool(true)
settype() accepts the same historical names as gettype()
Valid type strings for `settype()` include `"integer"`, `"int"`, `"float"`, `"double"`, `"string"`, `"boolean"`, `"bool"`, `"array"`, `"object"`, and `"null"` — PHP is lenient here and accepts both the modern and historical spellings, unlike `gettype()`'s output which is fixed.
The is_*() family

For everyday type checks, the is_*() functions are almost always the better tool: each one asks a single, specific question and returns a plain bool, which reads clearly inside an if and avoids ever comparing against a magic string.

is_int, is_string, is_array, and friends

PHP
<?php
$value = [1, 2, 3];

var_dump(is_array($value));   // bool(true)
var_dump(is_int($value));     // bool(false)
var_dump(is_string("hi"));    // bool(true)
var_dump(is_bool(false));     // bool(true)
var_dump(is_null(null));      // bool(true)
var_dump(is_numeric("3.14")); // bool(true) - numeric string, not just int/float
var_dump(is_callable('strlen')); // bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
  • is_numeric() returns true for both actual numbers and numeric strings such as "3.14" or "1e5", which makes it the right check before running arithmetic on user input.

  • is_int() (alias is_integer(), is_long()) only returns true for real integers — is_int("5") is false, even though the string is numeric.

  • is_array() is the standard guard before calling array-only functions like count() or array_map() on a value that might not be an array.

  • is_a() and instanceof check whether an object belongs to a specific class, which is a separate concern from these scalar/compound checks.

var_dump() vs print_r() vs var_export()

PHP has three built-in functions for turning a value into readable text, and each is suited to a different job. var_dump() is the most detailed, showing both type and value, including nested structures. print_r() is friendlier for a quick look at an array's shape, but drops type information for scalars. var_export() prints a value as valid PHP code, which means its output can be copy-pasted directly into a script.

Comparing the three output functions

PHP
<?php
$data = ["id" => 7, "active" => true, "tag" => null];

var_dump($data);
echo "---\n";
print_r($data);
echo "---\n";
var_export($data);
array(3) {
  ["id"]=>
  int(7)
  ["active"]=>
  bool(true)
  ["tag"]=>
  NULL
}
---
Array
(
    [id] => 7
    [active] => 1
    [tag] =>
)
---
array (
  'id' => 7,
  'active' => true,
  'tag' => NULL,
)

Function

Shows types?

Valid PHP output?

Best for

var_dump()

Yes, with string lengths

No

Deep debugging

print_r()

No (booleans/null lose distinction)

No

Quick array shape at a glance

var_export()

Yes

Yes

Generating cached PHP arrays or config

Tip
Reach for a specific `is_*()` function whenever you are branching on type in real logic — it is both faster to read and less error-prone than comparing `gettype($value) === 'integer'` against a string that is easy to misspell.