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:

//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 function – testing variable type

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.

// 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:

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

Find whether the type of a variable is integer:

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

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

Finds whether the type of a variable is float

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

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

Finds out whether a variable is a Boolean

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

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

Find whether the type of a variable is string

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

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

Finds whether a variable is array

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

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

Finds whether a variable is object

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

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

Finds whether a variable is a number

Such as integer, float, binary, hexadecimal, octal or a numeric string.

// 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

Finds whether a variable is a scalar

Scalar variables are those containing an integer, float, string or boolean.

$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 name of the function.

Advertisement:
Advertisement: