The gettype Function

The gettype function returns a string indicating the type of the passed variable. It accepts one parameter - the variable whose type needs to be determined.

Syntax

phpgettype(mixed $var): string

Return Values

Type Description
'boolean' Boolean values
'integer' Integers
'double' Floating point numbers
'string' Strings
'array' Arrays
'object' Objects
'resource' Resources
'NULL' Null values
'unknown type' Unknown types

Example

Determining the type of an integer:

php<?php
  echo gettype(123);
?>

Code execution result:

php'integer'

Example

Determining the type of a floating point number:

php<?php
  echo gettype(3.14);
?>

Code execution result:

php'double'

Example

Determining the type of a string:

php<?php
  echo gettype('hello');
?>

Code execution result:

php'string'

Example

Determining the type of a boolean value:

php<?php
  echo gettype(true);
?>

Code execution result:

php'boolean'

Example

Determining the type of an array:

php<?php
  echo gettype(['a', 'b', 'c']);
?>

Code execution result:

php'array'

Example

Determining the type of NULL:

php<?php
  echo gettype(null);
?>

Code execution result:

php'NULL'

Example

Let's check the variable type after conversions:

php<?php
  $var = '123';
  echo gettype($var) . "\n";
  
  $var = (int)$var;
  echo gettype($var) . "\n";
  
  $var = (float)$var;
  echo gettype($var) . "\n";
?>

Code execution result:

php'string'
'integer'
'double'

See Also

  • the is_int function,
    which checks if a variable is an integer
  • the settype function,
    which sets the type of a variable