PHP gettype() function

In this chapter you will learn:

  1. Description for PHP gettype() function
  2. Syntax for PHP gettype() function
  3. Return for PHP gettype() function
  4. Example - Find out the data type

Description

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

Syntax

PHP gettype() function has the following syntax.

gettype(var);

Return

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

Example


<?PHP/* ja v  a 2 s.co  m*/
   $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.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP intval() function
  2. Syntax for PHP intval() function
  3. Parameter for PHP intval() function
  4. Return value for PHP intval() function
  5. Example - Find out the int value
Home » PHP Tutorial » PHP Data Type Functions
PHP boolval() function
PHP floatval() function
PHP gettype() function
PHP intval() function
PHP is_array(value) function
PHP is_bool() function
PHP is_float() function
PHP is_int() function
PHP is_null() function
PHP is_numeric() function
PHP is_object() function
PHP is_resource() function
PHP is_string() function
PHP isset() function
PHP settype() function
PHP strval() function