Categories
PHP

Determine Variable Data Type

PHP is known as a loosely-typed language. It converts a variable’s data type automatically, depending on the context in which the variable is used. For example, you can initialize a variable with an integer value, add a float value to it, thereby turning it into a float, then join it onto a string value to produce a string:

<?php
//integer
$var = 1;

//float
$var += 0.1; //1.1

//string
$var .= ' abc'; // 1.1 abc

In contrast, many other languages, such as Java, are strongly-typed, once you set the type of a variable in Java, it must always contain data of that type.

gettype: check the type of a variable

You can check the type of a variable by using the gettype() function. Just pass in the variable whose type you want to test and get the variable’s type as a string.

<?php
// Declares the $var variable without initializing it
$var;
echo gettype($var); // NULL

$var = 2;
echo gettype($var); // integer

$var = 2.3;
echo gettype($var); // double

$var = 2.0;
echo gettype($var); // double

$var = "BrainBell";
echo gettype($var); // string

//Empty array
$var = []; //or array();
echo gettype($var); // array

$var = dir('./');
echo gettype($var); // resource

The $var variable initially has a type of null, because it has been created but not initialized by assigning a value. After setting $var's value to 2, its type changes to integer.

Setting $var to 2.3 changes its type to double (which in PHP means the same as float, because all PHP floating-point numbers are double-precision). In PHP, a floating-point value is simply a value with a decimal point. So 2.0 become a double (float) rather than an integer.

When setting $var to “BrainBell” changes its type to string, finally, setting $var to empty array alerts its type to array.

You can also test a variable for a specific data type using PHP’s type testing functions:

FunctionDescription
is_int( )Returns true if the value is an integer
is_float( )Returns true if the value is a float
is_string( )Returns true if the value is a string
is_bool( )Returns true if the value is a boolean
is_array( )Returns true if the value is an array
is_object( )Returns true if the value is an object
is_resource()Returns true if the value is a resource
is_null( )Returns true if the value is a null
is_numeric( )Returns true if the value is a number (such as a numeric string or float)
is_scalar( )Returns true if the value is a null

is_int

Find whether the type of a variable is an integer:

<?php
$var = 3;
echo is_int($var);
// prints 1 for true

$var = "3";
echo is_int($var);
// prints nothing for false

is_int function example 2:

<?php
 $var = 5;
 if (is_int($var)){
  echo "The type of $var is integer.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_float

Finds whether the type of a variable is float:

<?php
$var = 3.1;
echo is_float($var);
//prints 1 for true

$var = 3;
echo is_float($var);
// prints nothing for false

is_float function example 2:

<?php
 $var = 5;
 if (is_float($var)){
  echo "The type of $var is double.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_bool

Finds out whether a variable is a Boolean

<?php
$var = true;
echo is_bool($var);
// prints 1 for true

$var = 1;
echo is_bool($var);
// prints nothing for false

is_bool function example 2:

<?php
 $var = 0.5;
 if (is_bool($var)){
  echo "The type of $var is boolean.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_string

Check if the value is of type string, example-1:

<?php
$var = "3";
echo is_string($var);
// prints 1 for true

$var = 3;
echo is_string($var);
// prints nothing for false

is_string function example-2:

<?php
 $var = '5';
 if (is_string($var)){
  echo "The type of $var is string.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_array

Example-1:

<?php
$var = array("an","array");
echo is_array($var);
// prints 1 for true

$var = "a string";
echo is_array($var);
// prints nothing for false

is_array function example 2:

<?php
 $var = [1,2,3];
 if (is_array($var)){
  echo "The type of $var is array.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_object

Finds whether a variable is an object, example-1:

$var = new stdClass();
echo is_object($var);
// prints 1 for true

$var = array("an","array");
echo is_object($var);
// prints nothing for false

is_object function example 2:

<?php
 $var = 5;
 if (is_object($var)){
  echo "The type of $var is an object.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_numeric

Finds whether a variable is a number, example-1:

Such as integerfloatbinaryhexadecimaloctal or a numeric string.

<?php
// numeric string
$var = "3";
echo is_numeric($var);

// float string
$var = "3.25";
echo is_numeric($var);

// integer
$var = 3;
echo is_numeric($var);

// float
$var = 3.25;
echo is_numeric($var);

// positive numeric string
$var = "+3";  
echo is_numeric($var);

// negative numeric string
$var = "-3.25";
echo is_numeric($var);

// positive integer
$var = +3;  
echo is_numeric($var);

// negative float
$var = -3.25;  
echo is_numeric($var);

// 55
$var = 5.5e1;
echo is_numeric($var);

// binary number (decimal value is: 55)
$var = 0b00110111;
echo is_numeric($var);

// hexadecimal (decimal value is: 55)
$var = 0x37; 
echo is_numeric($var);

// octal (decimal value is: 55)
$var = 067;
echo is_numeric($var);

// All results print 1 for true

is_numeric function example 2:

<?php
 $var = 5;
 if (is_numeric($var)){
  echo "The type of $var is a number.";
 }
 else {
  echo "The type of $var is ". gettype($var);
 }

is_scalar

Finds whether a variable is a scalar. Scalar variables are those containing an integerfloatstring or boolean. Example-1:

<?php
$var = 1;
echo is_scalar($var);
// prints 1 for true

$var = 1.1;
echo is_scalar($var);
// prints 1 for true

$var = "hello world";
echo is_scalar($var);
// prints 1 for true

$var = false;
echo is_scalar($var);
// prints 1 for true

$var = array(1,2,3);
echo is_scalar($var);
// prints noting for false

$var = new stdClass();
echo is_scalar($var);
// prints noting for false

All the functions return a Boolean value of true or false for the variable $var depending on whether it matches the variable type that forms the function’s name.

Debugging with print_r( ) and var_dump( )

PHP provides the print_r( ) and var_dump( ) functions, which print the type and value of an expression in a human-readable form:

//Syntax
print_r(mixed expression)
var_dump(mixed expression [, mixed expression ...])

These functions are useful for debugging a script, especially when dealing with arrays or objects. To test the value and type of $variable at some point in the script, the following code can be used:

$variable = 15;
var_dump($variable);

This prints: int(15)

While the var_dump( ) function allows multiple variables to be tested in one call, and provides information about the size of the variable contents, print_r( ) provides a more concise representation of arrays and objects. These functions can be used on variables of any type, and we use them throughout these tutorials to help illustrate the results of our examples.


Data types in PHP:

  1. Data Types
  2. Determine Variable Data Type
  3. Type Conversion: Convert variable data type
  4. Type Declaration
  5. Strict Typing Mode
  6. Testing, setting, and unsetting variables