PHP Data Types
PHP Data Types
PHP supports 8 primary data types that you can use to store different kinds of values.
1. String
A string is a sequence of characters.
✔ Strings can be created using single (‘ ’) or double (“ ”) quotes.
2. Integer
Whole numbers (positive or negative) without decimals.
3. Float (Double)
Numbers with decimal values.
4. Boolean
Can be either:
-
true -
false
Useful in conditions, loops, and logical expressions.
5. Array
A variable that can store multiple values.
PHP has:
-
Indexed arrays
-
Associative arrays
-
Multidimensional arrays
6. Object
Used for object-oriented programming (OOP).
Created from classes.
7. NULL
A special type that represents no value.
Means the variable exists but has no value.
8. Resource
Used to store references to external resources like:
-
Database connections
-
File handles
-
Curl handlers
Example (MySQL connection):
Here $conn is a resource.
Checking Data Type — var_dump()
You can inspect the type and value using:
Output example:
Automatic Type Conversion (Type Juggling)
PHP is loosely typed → it automatically converts types.
Type Casting
Manually convert data types:
Summary Table
| Data Type | Example |
|---|---|
| String | "Hello" |
| Integer | 25 |
| Float | 10.5 |
| Boolean | true / false |
| Array | [1,2,3] |
| Object | new ClassName() |
| NULL | null |
| Resource | DB connection |
If you want, I can explain the next topic:
👉 PHP Strings
👉 PHP Constants
👉 PHP Operators
👉 PHP Arrays
