PHP Tutorial - PHP gettype() function

We can determine the type of a variable by using PHP's gettype() function.


PHP gettype() function has the following syntax.



The function then returns the variable's type as a string.


<?PHP//ww  w  .  j  a v  a 2  s  . c om
   $test_var; // Declares the $test_var variable without initializing it 
   echo gettype( $test_var ) . "\n"; // Displays "NULL" 
   $test_var = 15; 
   echo gettype( $test_var ) . "\n"; // Displays "integer" 
   $test_var = 1.23; 
   echo gettype( $test_var ) . "\n"; // Displays "double" 
   $test_var = "Hello, world!"; 
   echo gettype( $test_var ) . "\n"; // Displays "string" 

The code above generates the following result.

The $test_var variable initially has a type of null, because it has been created but not initialized.

After setting $test_var's value to 15, its type changes to integer.

Setting $test_var to 1.23 changes its type to double.

Finally, setting $test_var to "Hello, world!" alters its type to string.

In PHP, a floating - point value is simply a value with a decimal point. So if 15.0 was used instead of 15 in the preceding example, $test_var would become a double, rather than an integer.