PHP Data Types
Variables can store different types of data, so the data types define the type of data a variable can store. PHP allows 8 different types of data types. All of them are discussed below.
- Integer
- Float
- String
- Boolean
- Array
- Objects
- NULL
- resource
1. Integer
An integer is any number including 0, positive numbers, and negative numbers. It can never be a fraction, a decimal, or a percent. So it holds only whole numbers, i.e., numbers without fractional parts or decimal points. And the range of integers must lie between -2^31 to 2^31.
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.
Note: Single and double quotes both are treated differently. Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
4. Boolean
Booleans are the simplest data type. 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. You will learn more about conditional testing in a later chapter of this tutorial.
5. Array
A PHP array stores multiple values in one single variable:
6. Objects
Classes and objects are the two main aspects of object-oriented programming. So Objects are the instances of user-defined classes that can store both values and functions. When the objects are created, they inherit all the properties and behaviors from the class, having different values for all the properties.
7. NULL
Null is a special data type that can have only one value i.e., NULL. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters.