22 of 410 menu

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

gettype(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 echo gettype(123); ?>

Code execution result:

'integer'

Example

Determining the type of a floating point number:

<?php echo gettype(3.14); ?>

Code execution result:

'double'

Example

Determining the type of a string:

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

Code execution result:

'string'

Example

Determining the type of a boolean value:

<?php echo gettype(true); ?>

Code execution result:

'boolean'

Example

Determining the type of an array:

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

Code execution result:

'array'

Example

Determining the type of NULL:

<?php echo gettype(null); ?>

Code execution result:

'NULL'

Example

Let's check the variable type after conversions:

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

Code execution result:

'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
byenru