Data types are a crucial part of any programming language because they help us to categorize the data in different ways according to their behaviour. A data type has a different operational capabilities and different sizes also. We have variables which store data and this data comes in different data types, like integer, float, string, etc. We will go through all these different PHP data types in this tutorial.
In PHP, the programmer doesn't declare data types as other programming languages we just use the dollar '$'
sign with every variable and give each variable a value, then the PHP Parser itself decides the data type of the value.
PHP supports eight data types: Integer, Float, String, Booleans, Array, Object, resource and NULL.
Some of these are called scalar
, vector
or special data types
, we will look at this in the tutorial below.
Let's start:
A Scalar data type is the one which holds only a single value. PHP has 4 scalar data types, which are integer, float string and boolean. Let's go through these one by one:
Integers holds only positive and negative integers including zero. These numbers are pure integers with no decimal values and fractional part.
The range of integers is between -2,147,483,648
and 2,147,483,647
.
These integers can be decimal(base 10), hexadecimal(base 16) or octal(base 8).
The prefix for hexadecimal is '0' and for octal is '0', look at the example below to see how you can use these different integers.
Float data type holds the values with fractional part
or you can say with decimal values.
These includes positive or negative values with decimal part. Floats are also called 'double'
. Look at the example below to see how float data type works:
String is a collection of characters
, it holds letters and even numbers.
A string is written within single or double quotes but single quotes will give different result while printing variables, you will learn more about it 'here'.
Look at the example below to see how string works.
A Boolean data type has only two values TRUE and FALSE
.
We use these values in conditional statements, you will get familiar with these in the upcoming tutorials, so don't worry about these.
Just remember that successful events return TRUE otherwise FALSE.
Other than these, NULL type or 0 is also considered as FALSE in Boolean. You won't get it completely because it needs practical approach, later in the tutorial when we will use them then it would be easy for you to understand their use.
Compound data types can hold multiple values
.
These are combination of Scalar data types. In PHP we will learn about two Compound data types which are array and object.
Let's go through these one by one:
Array is a data type which can hold multiple values of same data type. Arrays are very helpful when you want to store data of a single group of similar type, for example, Student names of a particular class. These multiple values can be easily accessed by using an index starting from 0, that's why it is also called indexed collection of data values. Look at the example below to get a glimpse of how Array works:
An object is a comples data type which can hold different values
and also can store information about how to process them.
In simple terms it can hold variables and methods also. Objects are created using 'new'
keyword.
If you haven't studied Object Oriented Programming then you won't understand objects, so stay with us, in the upcoming tutorials your all doubts will be cleared.
Every object is related to a class, without a class there cannot be an object.
A class can have multiple objects, storing different values each time and allocating separate space for each variable. Each object is independent of the other objects of the same class.
Look at the example below to see how a class is defined and then the object of that class is created by the help of 'new'
keyword.
NULL is a very special and uniques kind of data type as it can hold only one value
i.e., NULL
.
It is not case sensitive you can write it as 'null' also. If we give any variable a NULL value that means that it we hasn't assigned any value to the variable.
This can be used if we don't want to assign any value to any variable right now but later on.
Resources in PHP are not an exact data type but these are used to hold references to outside resources like a file or a database connection
.
These are kind of special handlers to opened database connection or opened files.
It is an advance topic, so it will be difficult to understand it in this stage but we are giving you an introduction.
In the example below, the file()
function opened a file and that is reference is getting saved in the variable, and same for the database connection with mysql_connect()
function.
These variables are resource variables which holds the references, in the whole program these variables can be used as a reference to these functions.
Follow Us: