PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
- String
- Integer
- Float [floating point numbers - also called double]
- Boolean
- Array
- Object
- NULL
- Resource
PHP String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
Example
Try it Yourself »
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
- An integer must have at least one digit
- An integer must not have a decimal point
- An integer can be either positive or negative
- Integers can be specified in: decimal [base 10], hexadecimal [base 16], octal [base 8], or binary [base 2] notation
In the following example $x is an integer. The PHP var_dump[] function returns the data type and value:
PHP Float
A float [floating point number] is a number with a decimal point or a number in exponential form.
In the following example $x is a float. The PHP var_dump[] function returns the data type and value:
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump[] function returns the data type and value:
Example
Try it Yourself »
You will learn a lot more about arrays in later chapters of this tutorial.
PHP Object
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Let's assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.
When the individual objects [Volvo, BMW, Toyota, etc.] are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
If you create a __construct[] function, PHP will automatically call this function when you create an object from a class.
Example
Try it Yourself »
PHP NULL Value
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
Variables can also be emptied by setting the value to NULL:
PHP Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
A common example of using the resource data type is a database call.
We will not talk about the resource type here, since it is an advanced topic.
PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:
- Scalar Types [predefined]
- Compound Types [user-defined]
- Special Types
PHP Data Types: Scalar Types
It holds only single value. There are 4 scalar data types in PHP.
- boolean
- integer
- float
- string
PHP Data Types: Compound Types
It can hold multiple values. There are 2 compound data types in PHP.
- array
- object
PHP Data Types: Special Types
There are 2 special data types in PHP.
- resource
- NULL
PHP Boolean
Booleans are the simplest data type works like switch. It holds only two values: TRUE [1] or FALSE [0]. It is often used with conditional statements. If the condition is correct, it returns TRUE otherwise FALSE.
Example:
Output:
PHP Integer
Integer means numeric data with a negative or positive sign. It holds only whole numbers, i.e., numbers without fractional part or decimal points.
Rules for integer:
- An integer can be either positive or negative.
- An integer must not contain decimal point.
- Integer can be decimal [base 10], octal [base 8], or hexadecimal [base 16].
- The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.
Example:
Output:
Decimal number: 34 Octal number: 163 HexaDecimal number: 69
PHP Float
A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.
Example:
Output:
Addition of floating numbers: 73.812
PHP String
A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters.
String values must be enclosed either within single quotes or in double quotes. But both are treated differently. To clarify this, see the example below:
Example:
Output:
Hello Javatpoint Hello $company
PHP Array
An array is a compound data type. It can store multiple values of same data type in a single variable.
Example:
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
You will learn more about array in later chapters of this tutorial.
PHP object
Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.
Example:
Output:
Bike Model: Royal Enfield
This is an advanced topic of PHP, which we will discuss later in detail.
PHP Resource
Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.
This is an advanced topic of PHP, so we will discuss it later in detail with examples.
PHP Null
Null is a special data type that has only one value: NULL. There is a convention of writing it in capital letters as it is case sensitive.
The special type of data type NULL defined a variable with no value.
Example:
Output: