PHP | gettype() Function

The gettype() function is an inbuilt function in PHP which is used to get the type of a variable. It is used to check the type of existing variable.

Syntax:

string gettype ( $var )

Parameter: This function accepts a single parameter $var. It is the name of variable which is needed to be checked for type of variable.



Return Value: This function returns a string type value. Possible values for the returned string are:

  • boolean
  • integer
  • double (for historical reasons “double” is returned in case of float)
  • string
  • array
  • object
  • resource
  • NULL
  • unknown type

Below programs illustrate the gettype() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate gettype() function
  
$var1 = true; // boolean value 
$var2 = 3; // integer value
$var3 = 5.6; // double value
$var4 = "Abc3462"; // string value
$var5 = array(1, 2, 3); // array value
$var6 = new stdClass; // object value
$var7 = NULL; // null value
$var8 = tmpfile(); // resource value
  
echo gettype($var1)."\n";
echo gettype($var2)."\n";
echo gettype($var3)."\n";
echo gettype($var4)."\n";
echo gettype($var5)."\n";
echo gettype($var6)."\n";
echo gettype($var7)."\n";
echo gettype($var8)."\n";
  
?>

chevron_right


Output:

boolean
integer
double
string
array
object
NULL
resource

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate gettype() function
  
$var1 = "GfG"
$var2 = 10 % 7;
$var3 = pow(10, 2);
$var4 = pow(10, 0.5);
$var5 = sqrt(9);
  
echo gettype($var1)."\n";
echo gettype($var2)."\n";
echo gettype($var3)."\n";
echo gettype($var4)."\n";
echo gettype($var5);
?>

chevron_right


Output:

string
integer
integer
double
double

Reference: http://php.net/manual/en/function.gettype.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.