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
$number = 10;
$string = 'Hello, world!';
$array = [1, 2, 3];
$object = new stdClass();
echo gettype($number); // 'integer'
echo gettype($string); // 'string'
echo gettype($array); // 'array'
echo gettype($object); // '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 represents one of two possible values: true
or false
. It is primarily used in conditional statements and logical operations.
Here is an example to illustrate:
$var1 = true;
$var2 = false;
$type1 = gettype($var1); // $type1 will contain 'boolean'
$type2 = gettype($var2); // $type2 will contain 'boolean'
// Mainly used in conditional statements and logical operations
if ($var1) {
echo '$var1 is true.';
} else {
echo '$var1 is false.';
}
if ($var2) {
echo '$var2 is true.';
} else {
echo '$var2 is false.';
}
When checking for the 'boolean'
data type string, it is recommended to use the is_bool()
function. This function is specific to the boolean type, making it more explicit and safer than using gettype()
for this purpose.
$var2 = false;
// Use is_bool() to check if the variable is a boolean
if (is_bool($var1)) {
echo '$var1 is a boolean.';
} else {
echo '$var1 is not a boolean.';
}
if (is_bool($var2)) {
echo '$var2 is a boolean.';
} else {
echo '$var2 is not a boolean.';
}
The is_bool()
function checks the data type of a variable and returns true
if it is of type boolean, and false
otherwise.
'integer'
The 'integer'
string represents the integer data type. This includes all whole numbers: positive integers, negative integers, and zero.
Here is an example to illustrate:
$num1 = 10;
$num2 = -10;
$num3 = 0;
echo gettype($num1); // 'integer'
echo gettype($num2); // 'integer'
echo gettype($num3); // 'integer'
When checking for the 'integer'
data type string, it is recommended to use the is_int()
function. This function is specific to the integer type, making it more explicit and safer than using gettype()
for this purpose.
$num1 = 10;
$num2 = -10;
$num3 = 0;
echo is_int($num1); // true
echo is_int($num2); // true
echo is_int($num3); // true
The is_int()
function checks the data type of a variable and returns true
if it is of type integer, and false
otherwise.
'double'
The 'double'
string represents the floating-point data type. Floating-point numbers are numbers with decimal points, unlike integers which do not have a fractional part.
Here is an example to illustrate:
$num1 = 10.5;
$num2 = -10.5;
$num3 = 0.0;
echo gettype($num1); // 'double'
echo gettype($num2); // 'double'
echo gettype($num3); // 'double'
When checking for the 'double'
data type string, it is recommended to use the is_float()
function. This function is specific to the floating-point type, making it more explicit and safer than using gettype()
for this purpose.
$num1 = 10.5;
$num2 = -10.5;
$num3 = 0.0;
echo is_float($num1); // true
echo is_float($num2); // true
echo is_float($num3); // true
The is_float()
function checks the data type of a variable and returns true
if it is of type floating-point, and false
otherwise.
'string'
The 'string'
string represents the string data type. Strings must be enclosed in either single quotes ('
) or double quotes ("
).
Here is an example to illustrate:
$str1 = 'Hello, world!';
$str2 = 'Welcome! Nice to meet you!';
$str3 = '';
echo gettype($str1); // 'string'
echo gettype($str2); // 'string'
echo gettype($str3); // 'string'
When checking for the 'string'
data type string, it is recommended to use the is_string()
function. This function is specific to the string type, making it more explicit and safer than using gettype()
for this purpose.
$str1 = 'Hello, world!';
$str2 = 'Welcome! Nice to meet you!';
$str3 = '';
echo is_string($str1); // true
echo is_string($str2); // true
echo is_string($str3); // true
The is_string()
function checks the data type of a variable and returns true
if it is of type string, and false
otherwise.
'array'
The 'array'
string represents the array data type. PHP arrays come in two main types: indexed arrays (numeric keys) and associative arrays (key-value pairs).
Here is an example to illustrate:
$arr1 = [1, 2, 3];
$arr2 = ['Hello', 'world'];
$arr3 = [];
$arr4 = [
'name' => 'John Doe',
'age' => 30
];
echo gettype($arr1); // 'array'
echo gettype($arr2); // 'array'
echo gettype($arr3); // 'array'
echo gettype($arr4); // 'array'
When checking for the 'array'
data type string, it is recommended to use the is_array()
function. This function is specific to the array type, making it more explicit and safer than using gettype()
for this purpose.
$arr1 = [1, 2, 3];
$arr2 = ['Hello', 'world'];
$arr3 = [];
$arr4 = [
'name' => 'John Doe',
'age' => 30
];
echo is_array($arr1); // true
echo is_array($arr2); // true
echo is_array($arr3); // true
echo is_array($arr4); // true
The is_array()
function checks the data type of a variable and returns true
if it is of type array, and false
otherwise.
'object'
The 'object'
string represents the object data type. In PHP, objects are instances of classes, where a class defines the properties and methods of the object. An object encapsulates both data and behavior as defined by its class.
Here is an example to illustrate:
class MyClass {
public $name = 'John Doe';
public $age = 30;
}
$obj1 = new MyClass();
$obj2 = clone $obj1;
echo gettype($obj1); // 'object'
echo gettype($obj2); // 'object'
When checking for the 'object'
data type string, it is recommended to use the is_object()
function. This function is specific to the object type, making it more explicit and safer than using gettype()
for this purpose.
class MyClass {
public $name = 'John Doe';
public $age = 30;
}
$obj1 = new MyClass();
$obj2 = clone $obj1;
echo is_object($obj1); // true
echo is_object($obj2); // true
The is_object()
function checks the data type of a variable and returns true
if it is of type object, and false
otherwise.
'resource'
The 'resource'
string represents a resource data type used to interact with external resources. Examples include files, database connections, images, and network connections.
Here is an example to illustrate:
// Open a file
$fp = fopen('test.txt', 'w');
echo gettype($fp); // 'resource'
When checking for the 'resource'
data type string, it is recommended to use the is_resource()
function. This function is specific to the resource type, making it more explicit and safer than using gettype()
for this purpose.
// Open a file
$fp = fopen('test.txt', 'w');
echo is_resource($fp); // true
The is_resource()
function checks the data type of a variable and returns true
if it is of type resource, and false
otherwise.
'resource (closed)'
The 'resource (closed)'
string represents a resource that was previously active but has now been closed. This data type indicates that the resource is no longer active and cannot be used.
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 represents a variable that is either undefined or has no assigned value. A variable explicitly assigned the value null
also has the 'NULL'
data type.
Here is an example to illustrate:
$a = null;
$b;
echo gettype($a); // 'NULL'
echo gettype($b); // 'NULL'
When checking for the 'NULL'
data type string, it is recommended to use the is_null()
function. This function is specific to the NULL
type, making it more explicit and safer than using gettype()
for this purpose.
$a = null;
$b;
echo is_null($a); // true
echo is_null($b); // true
The is_null()
function returns true
if the given variable is null
, and false
otherwise.
'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.