What Are PHP Supported Data Types

What Are PHP Supported Data Types Data type is a common set of characteristics that are used as common name for data sharing. Hence, all programming language has it’s own data types.Boolean, integer, float, string, array and objects are the common PHP supported data types as other programming languages.

Since, you can use different data type specifiers to represent different data types in PHP. Therefore, data type specifier in PHP statement indicates that corresponding data type will be placed in that position. If you want to learn in detail about the implementation of different data type specifiers you may also refer to the post “How to start basic coding with PHP“.

PHP Supported Data Types

Since, PHP supports most of all common data types as other programming languages. Scalar data types and compound data types are the most common categories of PHP supported data types.

Scalar Data Types

PHP will support different types of scalar data types such as Boolean, Integer, Float and Strings. Here I have presented about the different scalar data types in detail.

Boolean

You may already know that, the Boolean data types supports only two values. True and False are two values that supports on Boolean data types. Following are the some examples of Boolean data types.

$tbool=true; // tbool is True
$tbool=-5; // tbool is True
$tool=0; // tbool is False

While coding with PHP, the type specifier “%b” represents binary numbers.This binary number will specify the Boolean data type to represent True or False.

Integer

Integer is the another PHP supported data type that does not contain fractional parts. Therefore, PHP supports integer values represented in decimal, octal and hexadecimal system. Following are some examples of different types of Integers.

95 is a decimal integer

0245 is a octal integer

oA4E is a hexadecimal integer

Since, different  type of specifiers  %c, %d, %o, %u, %x and %X represents different integers. I have also presented here the different types of specifiers along with their respective uses.

%c- Presented as a character corresponding to that ASCII value.

%d- Presented as a signed decimal number

%o- Presented as an octal number

%u- Presented  as an unsigned decimal number

%x-presented as a lowercase hexadecimal number

%X-presented as a uppercase hexadecimal number

Float

Float is the floating point number. Therefore, this type of number allows you to specify the numbers that contain fractional parts. Hence, you can use the floating point numbers to represent Monetary values, weights, distance etc. 4.23, 2.22, 3.0Fe, 2.10E+15 are some examples of floating point numbers.

The type specifier %f represents floating point number on the PHP statement. Following is an example with printf statement to output floating point number.

printf(“$%.2f, 22.3);

Hence, the above statement outputs floating point number $22.30 representing price with currency symbol $.

String

In PHP, string is a sequence of character delimited by single or double quotes. It also supports to assign specific character as a sting from another sting. Following are the valid examples of sting data types.

“This is PHP”

“234$%69ab”

$string1=”Hello”

$string2=$string1[4];

Compound Data Types

There are two PHP supported data types as compound data types in PHP. Compound data type allows you to aggregate multiple items to same type together. Array and objects are the compound data types used in PHP.

Array

Since, array data type in PHP allows you to aggregate a series of similar items together. So, it can be used as the indexed collection of similar types data values. Following is the example of array that represents the different programming languages.

$language[0]=”C”;

$language[1]=”Java”;

$language[2]=”PHP”

………………………….

$language[10]=”VB.Net”;

Object

Object is another compound data type that is supported by PHP. Since, it is similar but the advanced data type than array data type. It has the collection of dissimilar data types where as array has the similar data types. The object should be explicitly declared and placed within a class. Furthermore, the class definition includes different attributes and functions relevant to a data structure. Here is an example of creation and invocation of an object.

class Employee

{

public $name;

}

$employee=new Employee();

$employee->name=”John”;

$name=$employee->name;

In the above example, class Employee was declaring the the public property $name. Since, the statement $employee=new Employee() created the new object $employee. The statement $employee->name=”John” access the properties of the class and set the value “John” to the variable $name.