Data Types In PHP
The purpose of this article is to examine the different PHP data types with examples in more detail.
PHP is a dynamically typed language, so when you declare a variable, you do not have to specify its data type.
Based on the value assigned to a variable, PHP automatically determines the type of data that is stored there.
PHP Data Types
In order to store data, variables can be divided into different PHP data types, and each type of data can be used in a different way.
As far as PHP Data types are concerned, there are several types available:
- String
- Integer
- Float (floating point numbers – also called double)
- Boolean
- Array
- Object
- NULL
- Resource
PHP Data Type – String
A string consists of a sequence of characters. For example, a string could be “Elon Musk ”.
In a string, you can put any text between quotes.
Strings can be enclosed in either single quotes (‘) or double quotes (“).
To declare a string variable, simply assign a string value to it. For example:
Example: 
Example: 
PHP Data Type – Integer
An integer is a whole number, either positive or negative, without a fractional component.
In PHP, integers can range from -2,147,483,648 to 2,147,483,647.
Integer rules:
- One digit is required for an integer.
- No decimal point in an integer.
- Integers can be either positive or negative.
- Binary (base 2), octal (base 8), decimal (base 10), and hexadecimal (base 16) notation can be used to specify integers.
An integer is represented by $a in the following example. Data type and value are returned by PHP’s var_dump() function:
Example: 
Example: 
PHP Data Types – Float
The term float refers to a number with a decimal point or an exponential number.
In PHP, floating-point numbers are stored as double-precision floating-point values.
To declare a float variable, simply assign a number with a fractional component to it.
As an example, $a is a float. PHP’s var_dump() function returns the data type and value:
Example: 
Example: 
PHP Data Types – Boolean
There are only two possible values for boolean data types: true or false.
Booleans are commonly used in PHP conditional statements to determine whether a condition is true or false.
The only way to declare a boolean variable is to assign it the value TRUE or FALSE. Here are some examples:
$a = true; $b = false;
PHP Data Types – Array
In PHP Data types, An array is a data structure that can store multiple values, each accessible by a unique key.
In PHP, arrays can be indexed, associative, or a combination of both.
To declare an array, use the array keyword, or use square brackets ([]).
Here, $flowers is an array. PHP var_dump() returns the data type and value:
Example: 
Later in this tutorial, you will learn more about arrays.
PHP Data Types – Object
There are two main aspects of object-oriented programming that are classes and objects. Objects are data types that represent instances of classes.
PHP objects are used to store data and behavior (methods) associated with particular entities.
Class definitions can be defined as templates for defining objects, and objects can be defined as instances of classes.
Each object that is created inherits all the properties and behaviors that are found in the class. However, each object will have its own unique set of values for those properties in order to be unique.
Let us assume for a moment that we have a class named Flower.
The properties of a flower can include things like the type, the color, etc. For instance, we can define variables named $type, $color, etc., to hold values of these properties in the form of their values.
The individual objects (Sunflower, Rose, Jasmine, etc.) inherit all the properties and behaviors from the class when they are created, although the values of the properties will be different for every individual object.
In PHP, if you create a __construct() function, the function will automatically be called by PHP whenever an object is created from a class that uses the __construct() function.
Example: 
Example: 
Example Explanation:
In above example we have defines a class called “Pet”.
The class has two public properties, “name” and “weight“, and a constructor method that sets the values of these properties when an object of the class is created.
The class also has a method called “message” which returns a string that combines the values of the “name” and “weight” properties.
Finally, two objects of the “Pet” class are created and their “message” method is called, resulting in two different strings being printed.
The first object is created with the values “Dog” and “20” for the “name” and “weight” properties, respectively.
The second object is created with the values “Cat” and “18“.
PHP Data Types – NULL Value
The NULL value is a special data type in PHP that represents an absence of a value.
In other words, it is a value that indicates that a variable has been declared, but has not been assigned a value.
The NULL value is a unique value that is different from other data types, including an empty string or an integer with a value of 0.
The value of a variable can also be emptied by setting its value to NULL, as follows:
Example: 
In PHP, the NULL value can be useful in a number of situations.
For example, it can be used to initialize variables that will be later assigned a value.
It can also be used to represent missing or unknown values, or to indicate that a variable no longer holds a valid value.
To check if a variable has a value of NULL, you can use the is_null() function as follow:
Example: 
Example: 
PHP Data Types – Resource
There is no actual data type associated with the special resource type. References to external functions and resources are stored here.
Database calls are a common example of when you would use the resource data type.
Since this is a topic that is more advanced, we will not talk about resource types here.