Detecting the Data Type of a Variable

To find out what type a particular variable is,PHP offers the gettype() function, which accepts  a variable or value as argument.The following example illustrates this:


<?php

// define variables
$auth  = true;
$age   = 27;
$name  = 'Bobby';
$temp  = 98.6;

// returns String
echo gettype($name);

// return bolean
echo gettype($auth);

//return integer
echo gettype($age);

//return double
echo gettype($temp);

?>

PHP also supports a number of specialized functions to check if a variable or value belongs to a specific type.

Function What It Does
is_bool() Checks if a variable or value is Boolean
is_string() Check if a variable or value is a string
is_numeric() Check if a variable or value is numeric string
is_float() Check if a variable or value is a floating point number
is_int() Check if a variable or value is an integer
is_null() Check if a variable or value is NULL
is_array() Check if a variable or value is an array
is_object() Check if a variable or value is an object

This post is tagged: , ,


3 Responses to “Detecting the Data Type of a Variable”
  1. 07.23.2010

    thank you for the article!!!!

  2. 01.28.2011

    Technique add whatever show your blog!

  3. 02.04.2011

    Hi I love this post and it was so fabulous and I am gonna save it. I Have to say the Indepth analysis you have done is trully remarkable.Who goes that extra mile these days? Bravo.. Just another tip you caninstall a Translator Application for your Worldwide Audience :)


Leave a Reply