• PHP Video Tutorials

PHP - gettype() Function



Definition and Usage

The function gettype() returns type of a variable.

Syntax

string gettype ( mixed $value )

Parameters

Sr.No Parameter & Description
1

value

The variable whose type will be checked.

*mixed: mixed indicates that a parameter may accept multiple (but not necessarily all) types

Return Values

This function returns a string. 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) (as of PHP 7.2.0)

  • NULL

  • unknown type

Dependencies

PHP 4.0 and above

Example

Following example demonstrates the use of gettype() for different types of variables −

<?php
   $data = array(2, 2.0, NULL, new stdClass, 'tutPoint', true, array(1, 2, 3), tmpfile());

   foreach ($data as $value) {
      echo gettype($value);
   }
?>

Output

This will produce following result −

integer
double
NULL
object
string
boolean
array
resource
php_variable_handling_functions.htm
Advertisements