You are here: Daniel Dumas » Blog, PHP »

Detecting the data type of a PHP variable

Detecting the data type of a PHP variable

Detecting the datatype of a specific PHP variable can be done by using PHP gettype() function. refer below for example.

<?php

//define variables
$on = true;
$age = 20;
$name = "daniel";
$price = 99.99;

//Returns Boolean
echo gettype($on);

//Returns String
echo gettype($name);

//Returns Integer
echo gettype($age);

//Returns Double
echo gettype($price);

?>


You can also use other PHP functions to determine the datatypes of a variable

FUNCTION NAME
is_bool() Checks if a variable is a BOOLEAN
is_string() Checks if a variable is a STRING
is_numeric() Checks if a variable is a NUMERIC STRING
is_int() Checks if a variable is an INTEGER
is_array() Checks if a variable is an ARRAY
is_object() Checks if a variable is an OBJECT
is_null() Checks if a variable is NULL
is_float() Checks if a variable is a FLOAT

Discussion

  1. [...] the original here: Detecting the data type of a PHP variable Related ArticlesBookmarksTags PHP PHP is a computer scripting language. Originally [...]

  2. daniel says:

    *nosebleed. hehehe :D

    daniel’s last blog post..50 Random Stuff’s about Me.

  3. I hope you had a wonderful christmas and a prosperous 2009. More blessings to all of us. Cheers!!

  4. What about splitting obvious string “we have 20 goats”?
    This one is a string but after splitting to tokens with spaces would 20 still be a string or a integer when checking with gettype?

Leave a Reply





CommentLuv badge