gettype

Get the type of a variable

Syntax

gettype ( mixed $var ) : string

Parameters

var

The variable being type checked.

Return

Possible values for the returned string are:

"boolean"

"integer"

"double" (for historical reasons "double" is returned in case of a float, and not simply "float")

"string"

"array"

"object"

"resource"

"resource (closed)"

"NULL"

Examples

1

<?

$var = 0;

$return = gettype($var);

echo $return;

?>
integer

2

<?

$array = array(true, 0, 12.34, "abc", array(1, 2, 3), new stdclass, null);

foreach ($array as $value) {
    $return = gettype($value);

    echo $return . PHP_EOL;
}

?>
boolean
integer
double
string
array
object
NULL

3

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';
$mode = 'r';

$handle = fopen($filename, $mode);

    $return = gettype($handle);
    
    echo $return;

fclose($handle);

?>
resource

4

<?

$filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/txt/file.txt';
$mode = 'r';

$handle = fopen($filename, $mode);
fclose($handle);

$return = gettype($handle);
    
echo $return;

?>
resource (closed)

PHP

Home Menu