Data types available in PHP are of various types. A variable of a particular data type has its own use. In this article, we will cover up most of the internally defined data types in PHP.
PHP recognizes the following data types:
1) Integer
2) Float
3) String
4) Boolean
5) Array
6)Object
7) Null values
Integer [ Data Type ] in PHP
An integer data type supports integer between the following ranges:
-2,147,483,648 till 2,147,483,647.
The number cannot have decimal values. The number must be at least a single digit number which is either positive or negative.
Integers in PHP can take up any of the following three standards to represent integers:
- Decimal Notation (base 10).
- Octal-decimal notation (base 8).
- Hexa-decimal notation (base 16).
For example look at the following code snippet:
<?php $x=10; $y=13131132; echo “”.$x; echo “”.$y; ?>
The output will be
10 13131132
Float [Data Type] in PHP
A float data type is a numeric data type with a decimal point. In other words, you can say that the decimal point number can also be represented in exponential form.
Now check the following example for instance:
<?php $x=1.2121; echo “”.$x; ?>
The output will be:
1.2121
Boolean [Data Type] in PHP
A Boolean is data type which can have only two values associated with itself i.e. TRUE or FALSE (1 or 0). This type of data type finds its application in logical and conditional comparisons and testing.
See the following code snippet for example:
<?php $value=TRUE; $val2=FALSE; echo “”.$value; echo “”.$val2; ?>
The output of the above code will be:
TRUE FALSE
String [Data Type] in PHP
A string is basically a linear sequence of characters. In order to represent a string in PHP one must use either single or double quotes. The string can be any text inside the quotes.
For example look at the following code below for clarity:
<?php $val=”Welcome to Padhle.com!”; echo “”.$val; ?>
The output will be:
Welcome to Padhle.com!
Array [Data Type] in PHP
In PHP array is a predefined data type. So, what is an array?
An array is a container which can store multiple values of same data type under one single variable. One can access those multiple values through various traversal techniques. In PHP one can define an array with the help of array().
For example look at the following code snippet for better understanding:
<?php $list=array(1,2,3,4,5); echo “”$list[0]; ?>
The output will be:
1
Object [Data Type] in PHP
This is a data type which is an instance of the class in terms of Object Oriented Programming. In simple terms, an object stores the data and the operation on how to handle and process data. In PHP one must define the object externally. For that one must declare and define the class of the corresponding object.
The class is the required structure which consists of all the variables and methods, these components define an object and its properties.
Look at the following example for clarity:
<?php class objexample { $x=10; function display() { echo “”.$x; } } $a=new objexample(); $a->display(); ?>
In the above code on can see the declaration of the object “a” using the following syntax:
$obejct_name=new class_name();
“new” is a predefined keyword.
The output will be:
10
Null Value [Data Type] in PHP
In PHP we can also define null value to a variable. Null is a very different and a special data type. A variable assigned null is that variable that has no value assigned to it. In PHP if you create a variable without assigning any value to that variable, then PHP automatically assigns a NULL value to it. A variable of another data type can also be converted to NULL.
For example, see the following code snippet for better understanding:
<?php $x=null; echo “”.$x; var_dump($x); ?>
The output will be as follows:
NULL
var_dump() is a function that returns the data and the data type of a variable passed to it.
Hence we have seen the above-predefined data types available in PHP. For any further queries and doubts, post a comment in the section below.