Definition and Usage
- PHP Version
- 4+
The gettype() function returns the data type of the given value as a string.
This function is very useful for checking a variable's data type.
Basic Example
var_dump(gettype(10)); // string(7) "integer"
var_dump(gettype('Hello, world!')); // string(6) "string"
var_dump(gettype([1, 2, 3])); // string(5) "array"
var_dump(gettype(new stdClass())); // string(6) "object"
The settype() function changes the data type of the variable passed as an argument to the specified type.
Syntax
gettype(mixed $value): string
Parameters
$value |
The variable or value whose data type you want to check. |
|---|
Return Values
The function returns the data type of the variable as a string. The possible return values include:
These are the possible string values that gettype() can return. You can use this list to check the data type of a variable.
'boolean'
The 'boolean' string indicates that the data type is boolean, which can only hold one of two values: true or false. This type is primarily used for controlling flow in conditional statements and logical operations.
Here is an example to illustrate:
var_dump(gettype(true)); // string(7) "boolean"
var_dump(gettype(false)); // string(7) "boolean"
When checking whether a data type is boolean, it is recommended to use the is_bool() function. Since is_bool() applies specifically to the boolean type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_bool() function checks whether a given value is an integer.
It returns true if the value passed as an argument is an integer; otherwise, it returns false.
var_dump(is_bool(true)); // bool(true)
var_dump(is_bool(false)); // bool(true)
'integer'
The 'integer' string indicates that the data type is an integer, representing whole numbers without a decimal point. This includes all integer values, such as positive integers, negative integers, and zero.
Here is an example to illustrate:
var_dump(gettype(10)); // string(7) "integer"
var_dump(gettype(-10)); // string(7) "integer"
var_dump(gettype(0)); // string(7) "integer"
When checking whether a data type is an integer, it is recommended to use the is_int() function. Since is_int() applies specifically to the integer type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_int() function checks whether a given value is an integer.
It returns true if the value passed as an argument is an integer; otherwise, it returns false.
var_dump(is_int(10)); // bool(true)
var_dump(is_int(-10)); // bool(true)
var_dump(is_int(0)); // bool(true)
'double'
The 'double' string indicates that the data type is a floating-point number (double). Unlike integers, floating-point numbers are used to represent numeric values that include a decimal point and a fractional part.
Here is an example to illustrate:
var_dump(gettype(10.5)); // string(6) "double"
var_dump(gettype(-10.5)); // string(6) "double"
var_dump(gettype(0.0)); // string(6) "double"
When checking whether a data type is a floating-point number (double), it is recommended to use the is_float() function. Since is_float() applies specifically to floating-point types, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_float() function checks whether a given value is a float.
It returns true if the value passed as an argument is a float; otherwise, it returns false.
var_dump(is_float(10.5)); // bool(true)
var_dump(is_float(-10.5)); // bool(true)
var_dump(is_float(0.0)); // bool(true)
'string'
The 'string' string indicates that the data type is a string, which represents a sequence of characters. A string must be enclosed in either single quotes ('') or double quotes ("").
Here is an example to illustrate:
var_dump(gettype('Hello, world!')); // string(6) "string"
var_dump(gettype("Hello, world!")); // string(6) "string"
var_dump(gettype('')); // string(6) "string"
When checking whether a data type is a string, it is recommended to use the is_string() function. Since is_string() applies specifically to the string type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_string() function checks whether a given value is a string.
It returns true if the value passed as an argument is a string; otherwise, it returns false.
var_dump(is_string('Hello, world!')); // bool(true)
var_dump(is_string("Hello, world!")); // bool(true)
var_dump(is_string('')); // bool(true)
'array'
The 'array' string indicates that the data type is an array, which can hold multiple values in a single variable. PHP arrays include both indexed arrays (using numeric keys) and associative arrays (using string keys).
Here is an example to illustrate:
var_dump(gettype(array())); // string(5) "array"
var_dump(gettype([])); // string(5) "array"
var_dump(gettype([1, 2, 3])); // string(5) "array"
var_dump(gettype(['Hello', 'world'])); // string(5) "array"
var_dump(gettype(['name' => 'John Doe', 'age' => 30])); // string(5) "array"
When checking whether a data type is an array, it is recommended to use the is_array() function. Since is_array() applies specifically to the array type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_array() function checks whether a given value is an array.
It returns true if the value passed as an argument is an array; otherwise, it returns false.
var_dump(is_array(array())); // bool(true)
var_dump(is_array([])); // bool(true)
var_dump(is_array([1, 2, 3])); // bool(true)
var_dump(is_array(['Hello', 'world'])); // bool(true)
var_dump(is_array(['name' => 'John Doe', 'age' => 30])); // bool(true)
'object'
The 'object' string indicates that the data type is an object. In PHP, an object is an instance created from a class, encapsulating the properties (data) and methods (behaviors) defined within that class into a single unit.
class MyClass {
public $name = 'John Doe';
public $age = 30;
}
$obj1 = new MyClass();
$obj2 = clone $obj1;
var_dump(gettype($obj1)); // string(6) "object"
var_dump(gettype($obj2)); // string(6) "object"
When checking whether a data type is an object, it is recommended to use the is_object() function. Since is_object() applies specifically to the object type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_object() function checks whether a given value is an object.
It returns true if the value passed as an argument is an object; otherwise, it returns false.
class MyClass {
public $name = 'John Doe';
public $age = 30;
}
$obj1 = new MyClass();
$obj2 = clone $obj1;
var_dump(is_object($obj1)); // bool(true)
var_dump(is_object($obj2)); // bool(true)
'resource'
The 'resource' string indicates that the data type is a resource. A resource is a special data type used to interact with external resources outside of PHP, such as file handles, database connections, or network streams.
Here is an example to illustrate:
// Open a file
$fp = fopen('test.txt', 'w');
var_dump(gettype($fp)); // string(8) "resource"
When checking whether a data type is a resource, it is recommended to use the is_resource() function. Since is_resource() applies specifically to the resource type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_resource() function checks whether a given value is a resource.
It returns true if the value passed as an argument is a resource; otherwise, it returns false.
// Open a file
$fp = fopen('test.txt', 'w');
var_dump(is_resource($fp)); // bool(true)
'resource (closed)'
The 'resource (closed)' string represents a data type indicating that a resource previously used for interacting with external sources has been closed. This data type signifies that the resource is no longer active and is no longer accessible for use.
Here is an example to illustrate:
// Open a file
$fp = fopen('test.txt', 'w');
// Close the file handle
fclose($fp);
echo gettype($fp); // 'resource (closed)'
Note:
Do not use the is_resource() function to check for the 'resource (closed)' data type! The is_resource() function only checks for active resource types, so it will return false for a 'resource (closed)' value.
// Open a file
$fp = fopen('test.txt', 'w');
// Close the file handle
fclose($fp);
if (gettype($fp) == 'resource (closed)') {
echo "The file pointer is closed."; // Output
} else {
echo "The file pointer is still open.";
}
To check for the 'resource (closed)' data type, it is recommended to use gettype() to explicitly verify the variable's data type.
'NULL'
The 'NULL' string indicates that the data type is NULL. This is a special data type used to represent a variable with no assigned value, whether it has not been initialized or has been explicitly assigned a null value.
Here is an example to illustrate:
$a = null;
$b;
var_dump(gettype($a)); // string(4) "NULL"
var_dump(gettype($b)); // string(4) "NULL"
When checking whether a data type is NULL, it is recommended to use the is_null() function. Since is_null() applies specifically to the NULL type, it is more explicit and highly recommended over the gettype() function for validating this specific type.
Additional Explanation
The is_null() checks whether a given value is NULL.
It returns true if the value passed as an argument is NULL; otherwise, it returns false.
$a = null;
$b;
var_dump(is_null($a)); // bool(true)
var_dump(is_null($b)); // bool(true)
'unknown type'
Note:
In PHP versions prior to 7.2.0, the gettype() function returned the string 'unknown type' when checking a closed resource. This behavior caused ambiguity in representing closed resources. Starting with PHP 7.2.0, this was improved by changing gettype() to return 'resource (closed)' instead when checking closed resources. This change allows developers to more clearly identify when a resource variable is closed.
Since PHP 7.2.0, the 'unknown type' value no longer exists in PHP.