Understanding Data Types in PHP

Introduction

Most files kept in PHP issues fit in one of eight fundamental categories, known as data types. Some data types of variables can determine exactly what operations can be carried out for the variable's data, in addition to the volume of storage required to hold the data.

PHP can handle a number of scalar data types. Scalar facts signifies data that simply contain an individual value. The following reports on all of them, which includes examples:

Scalar Data Type Description Example
Integer It represents a whole number 11
Float It represents a floating point number. 5.76
String It represents set of characters. "MCN Solution"
Double It represents either true or false. true

In addition to a number of scalar types, PHP can handle a couple of compound types. Compound data is usually data that will consist of multiple values.The next table talks about PHP's compound type.

Compound Data Type Description
Array It specifies  group of the same data type that is used to store multiple values in a single variable.
Object It specifies a types that contain properties and methods.

Eventually, PHP can handle two unique data types, therefore called since they don't include scalar or compound data consequently, however have a very unique significance.

Special  Data Type Description
Resource It contain a reference to an external resource such as a file or database
Null It may only contain null as a value.

In PHP you need not write the data type before the variable name as with other languages like C#, C, C++, Java etc. We are now concerned with a loosely typed language.

Loosely Typing

PHP is known as a loosely-typed language. Because of this it is not especially popular with regards to the kind of information stored in a variable. It converts the variable's data type automatically, based on the context with which the variable is used. One example is, you possibly can initialize a variable with an integer value; add a float value to it, thus turning it into a float; then join it onto a string value to produce a longer string. In comparison, several languages, such as C# are strongly typed; once you set the type of variable in C#, it muse always contain data of that type.

PHP's loosely typing will be equally negative and positive. Within the plus side, it makes variables flexible; the same variable can easily be used in various situations. As an example, PHP will probably allow you to pass a floating-point value in a piece of code that is expected to be using an integer value. You probably would not see an error message, but you may discover that the output of your script is not quite what you expected! These types of errors can be hard to track down.


Testing the type of a variable


Using the "gettype()" function, you can determine the type of variable. In gettype(), you van simply pass the variable, that you want to test and this function returns the variable type as a string.

Example

The $test_variable initially has a type of null, because it has been created but not initialized. Then "$test_variable=15" executes, so it returns "integer", then "$test_variable=12.45" executes, so it returns "double", finally "$test_variable="MCN Solution""; executes, so it returns "string".


<?
php
$test_variable;// without
initilization

echo
gettype($test_variable)."</br>"; //returns "NUll"
$test_variable
=15;

echo
gettype($test_variable)."</br>";//returns "integer"
$test_variable
=12.45;

echo
gettype($test_variable)."</br>";//returns "double"
$test_variable="MCN Solution";

echo
gettype($test_variable);//returns "string"
?>

 
Output

check-value-in-php.gif

Specific function for specific data type testing

Here I also provide a list of PHP testing functions that test a variable for a specific data type.

Function Description
is_int(value) It return true if value is an integer.
is_float(value) It return true if value is a float.
is_string(value) It return true if value is a string.
is_bool(value) It return true if value is a Boolean.
is_array(value) It return true if value is an array.
is_object(value) It return true if value is an object.
is_resource(value) It return true if value is a resource.
is_null(value) It return true if value is null.

Changing a variable's Data Type

In the above, you also read how to modify variable's type by assigning various values to the variables. Now change the type of a variable without preserving the variabl's value as much as possible. You can simply use the PHP settype() function.


Example


<?
php
$test_variable =
9.23;

echo
$test_variable."</br>"; // Displays "9.23"
settype
( $test_variable, "string" );
echo
$test_variable."</br>"; // Displays "9.23"
settype
( $test_variable, "integer" );
echo
$test_variable."</br>"; // Displays "9"
settype
( $test_variable, "float" );
echo
$test_variable."</br>"; // Displays "9"
settype
( $test_variable, "boolean" );
echo
$test_variable."</br>"; // Displays "1"
?>


Output


settype-function-in-php.gif

Type Casting

Another way to change a variable's data type or say you want a variable's value to be treated as a specific type, is to use a technique known as type casting. To do that, you can simply put the name of the required data type in parentheses before the variable's  name.


Example


<?
php
$test_variable =
9.23;

echo
$test_variable."</br>"; // Displays "9.23"
echo
(int)$test_variable."</br>"; // Displays "9"
echo
(float)$test_variable."</br>"; // Displays "9.23"
echo
(boolean)$test_variable."</br>"; // Displays "1"
echo
(string)$test_variable."</br>"; // Displays "9.23"
?>


Output

type-cating-in-php.gif

The following list shows various types of casts in PHP.

Function Description
(int)value the value casts into an integer.
(float)value the value casts into float.
(string)value the value casts into string.
(bool)value the value casts into Boolean.
(array)value the value casts into an array.
(object)value the value casts into an object.