A PHP variable starts with a dollar sign, such as $price, and can hold a value of any type. PHP is loosely typed: the same variable can hold an integer now and a string later, and PHP converts values on its own when an operation needs a different type. That is convenient, but it also means you need tools to ask what a variable holds, convert it on purpose, and look inside it when something goes wrong.
The Variable Functions index has a guide for each of the 37 functions and language constructs in this area.
Test first, convert second.
Most type bugs come from guessing. Check a value with an is_* function or get_debug_type(), then convert it with an explicit cast, instead of relying on PHP to convert it silently.
Some of these are not functions.
isset(), empty() and unset() are language constructs. They look like functions, but you cannot call them through a variable name or pass them as callbacks. They are listed with the functions because you use them the same way.
Syntax
PHP Syntax
A quick tour of the calls you will use most. The comment on each line shows what it gives back:
PHP
$value = "42abc";
// Type tests: each returns bool
is_int($value); // false
is_numeric($value); // false
is_string($value); // true
// Casting and converting
intval($value); // int: 42
floatval("3.5kg"); // float: 3.5
strval(10); // string: "10"
boolval("0"); // bool: false
settype($value, "integer");// bool: true, $value is now int 42
// Existence (language constructs)
isset($value); // bool: true when set and not null
empty($value); // bool: true when unset or falsy
unset($value); // removes the variable
// Debugging
gettype(1.5); // string: "double"
get_debug_type(1.5); // string: "float" (PHP 8.0+)
var_dump($data); // prints type and value
print_r($data); // prints a readable form
var_export($data); // prints valid PHP code
// Storing as text
$text = serialize($data); // string
$back = unserialize($text, ['allowed_classes' => false]); // value or false
Reference
Type Checks (is_*)
Every is_* function takes one value and returns true or false. They never change the value and never raise a warning, so they are safe on any input:
Changes the variable itself and returns true or false, not the new value
A cast never fails, and that is the risk.
intval("abc") quietly returns 0, which looks the same as a real zero. When the input must be a number, test it with is_numeric() first and reject it if the test fails.
Existence
isset(), empty() and unset()
These three decide whether a variable exists and whether it holds anything useful. They do not raise a warning for a missing variable or array key, which makes them the right tools for optional input:
serialize() turns a value, including arrays and objects, into a string that can be stored in a file, a cache or a database. unserialize() turns that string back into the value. The format is specific to PHP, so other languages cannot read it.
Never unserialize data you do not trust.
A crafted string can create objects of any loaded class and run their magic methods, such as __wakeup() and __destruct(). This is called object injection. For data from users, cookies or other servers, use json_encode() and json_decode() instead. When you do call unserialize(), pass ['allowed_classes' => false] so that no objects are built.
Also remember that unserialize() returns false when the string is invalid, but false is also a valid result for the string b:0;. Compare against serialize(false) if you need to tell the two apart.
Code
PHP Examples
Three short scripts that use only fixed values, so the output is the same every time you run them.
Example 1 — Type checks and casting
Test five values with is_int(), is_numeric() and is_scalar(), then convert values with intval(), floatval(), strval(), boolval() and settype().
var_dump() prints the type of every value: int(7), bool(true), float(1.5). It is the only one of the three that shows the difference between 1 and true.
print_r() prints a shorter view. The boolean true shows as 1, so the type information is lost. The extra echo PHP_EOL leaves a blank line before the next output.
var_export() prints valid PHP code with quoted keys and a trailing comma, so you can paste it straight into a script. It adds no final line break, which is why the script prints PHP_EOL itself.
gettype() gives the legacy names double and NULL, while get_debug_type() (PHP 8.0 and later) gives float, null, and the class name stdClass.
serialize() writes each part with its type and length: a:3 is an array of 3 items, s:2:"id" is a 2-character string, i:7 an int, b:1 a bool and d:1.5 a float.
unserialize() with allowed_classes set to false builds the array again and never creates objects, which is the safe way to read stored text. The strict === comparison confirms that the copy is identical.
Continue
Next: PHP Variable Functions Index
The functions index lists all 37 functions and constructs in five groups. These are the ones most worth reading first:
isset() is true when a variable exists and is not null. empty() is true when a variable does not exist or holds a falsy value: an empty string, 0, 0.0, the string '0', null, false, or an empty array. A variable set to 0 is therefore set but also empty. Neither one raises a warning for a missing variable, which is why they are safe to use on input that may not exist.
Because its value is null. isset() treats null as not set. If you need to know whether an array key exists even when its value is null, use array_key_exists('key', $array) instead. For a plain variable, a null value and a missing variable look the same to isset().
For one argument they behave the same: both convert a value to an integer and stop at the first character that is not part of a number, so intval('42abc') is 42. The function form has an extra advantage: its second argument is a base, so intval('101', 2) is 5 and intval('1A', 16) is 26. It can also be passed as a callback, for example array_map('intval', $values).
gettype() returns legacy names such as 'integer', 'double', 'boolean' and 'NULL'. get_debug_type() (PHP 8.0 and later) returns the names you write in code: 'int', 'float', 'bool', 'null', and the class name for objects. Use get_debug_type() in error messages and logs, and gettype() only when you must stay compatible with older PHP versions.
No. Unserializing data you do not control can create objects of any loaded class and trigger their magic methods, which is called object injection and has led to real security holes. Use JSON (json_encode() and json_decode()) for data that crosses a trust boundary. If you must call unserialize(), pass ['allowed_classes' => false] or a short list of safe class names, and only for data you created yourself.
Use var_dump() when the exact type matters, because it prints the type and size of every value. Use print_r() for a short, readable view of arrays and objects. Use var_export() when you want output that is valid PHP code. Both print_r() and var_export() accept true as a second argument to return the text instead of printing it.