Home » PHP » PHP Data Types

Data Types in PHP

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:


Scalar data types

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:

PHP Integer:

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.

Example

<?php $dec1 = 12; $oct1 = 0243; $hexa1 = 0x45; echo "Decimal number: " .$dec1. "</br>"; echo "Octal number: " .$oct1. "</br>"; echo "HexaDecimal number: " .$hexa1. "</br>"; ?>

Output

Decimal number: 12
Octal number: 163
HexaDecimal number: 69

PHP Float/Double

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:

Example

<?php $n1 = 39.34; $n2 = 65.472; $sum = $n1 + $n2; echo "Addition of floating numbers: " .$sum; ?>

Output

Addition of floating numbers: 104.812

PHP String

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.

Example

<?php $company = "CodeRepublics"; /*both single and double quote statements will treat different */ echo "Hello $company"; echo "</br>"; echo 'Hello $company'; ?>

Output

Hello CodeRepublics
Hello $company

PHP Boolean

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.

Example

<?php if (TRUE) echo "This condition is TRUE."; if (FALSE) echo "This condition is FALSE."; ?>

Output

This condition is TRUE.

PHP Data Types: Compound Types

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:

PHP Arrays

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:

Example

<?php $bikes = array ("Royal Enfield", "Yamaha", "KTM"); var_dump($bikes); /* the var_dump() function returns the datatype and values */ echo "</br>"; echo "Array Element1: $bikes[0] </br>"; echo "Array Element2: $bikes[1] </br>"; echo "Array Element3: $bikes[2] </br>"; ?>

Output

array(3) { [0]=> string(13) "Royal Enfield" [1]=> string(6) "Yamaha" [2]=> string(3) "KTM" }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM

Objects

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.

Example

<?php class bike { function model() { $model_name = "Royal Enfield"; echo "Bike Model: " .$model_name; } } $obj = new bike(); $obj -> model(); ?>

Output

Bike Model: Royal Enfield

PHP Data Types: Special Types

NULL

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.

Example

<!DOCTYPE html> <html> <body> <?php $x = "Hello world!"; $x = null; var_dump($x); ?> </body> </html>

Output

NULL

PHP Resources

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.

Example

<?php /* Open a file for reading */ $handle = fopen("note.txt", "r"); var_dump($handle); echo "<br>"; /* Connect to MySQL database server with default setting */ $link = mysql_connect("localhost", "root", ""); var_dump($link); ?>











Follow Us: