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
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
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 $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)
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
$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()returnstruefor 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()(aliasis_integer(),is_long()) only returnstruefor real integers —is_int("5")isfalse, even though the string is numeric.is_array()is the standard guard before calling array-only functions likecount()orarray_map()on a value that might not be an array.is_a()andinstanceofcheck 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 $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 |
|---|---|---|---|
| Yes, with string lengths | No | Deep debugging |
| No (booleans/null lose distinction) | No | Quick array shape at a glance |
| Yes | Yes | Generating cached PHP arrays or config |