gettype(): Getting type of Variable

There are differnt types of variables used in PHP. We will get the type of variables by using gettype().
Here is an example.
echo " Type of variable  : ".gettype($my_var);
The output is here
Type of of variable : string
Based on the type of input variable we will get the output. Here is a list of output with example.
  1. Bool : True False ( two values only )
  2. Int : whole numbers like 5 , 356
  3. float(5.6) : with decimal numbers like 5.6, 2. , 3.89
  4. string(12) "" : ""
  5. array : Give more details, see example below
  6. NULL : null data

Difference between var_dump() and gettype()

PHP function var_dump() gives full details of the variable where gettype() only gives the type of variable. In our script we are processing different type of code based on variable type then gettype() can be used as we are only interested in type of the variable. Here are some examples to understand the difference. Output for each type of variables with both the functions are shown.
echo gettype($my_var); // boolean
echo "<br>";
echo var_dump($my_var); // bool(true)

echo gettype($my_var); // integer 
echo "<br>";
echo var_dump($my_var); // int(5)

echo gettype($my_var); //  double
echo "<br>";
echo var_dump($my_var); // flot(5.6)

echo gettype($my_var); // string 
echo "<br>";
echo var_dump($my_var); // string(12) ""

echo gettype($my_var); //  NULL
echo "<br>";
echo var_dump($my_var); // NULL

echo gettype($my_var); //  array 
echo "<br>";
echo var_dump($my_var); 
//array(3) { [0]=> string(5)"Mango" [1]=> string(6) "Banana" [2]=> string(5) "Apple" }
Declaring variables isset() empty() var_dump()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer