Data Types
Learn PHP data types — strings, integers, floats, booleans, arrays, objects, and null — and why choosing the right type matters for every variable.
Welcome to Data Types
Imagine you're building an online shopping application.
Different pieces of information need to be stored during the customer's shopping journey.
For example:
- Customer Name → Rahul Sharma
- Product Price → ₹49,999
- Quantity → 2
- Payment Status → Successful
- Delivery Date → 15 July 2026
Although all of these are data, they are not the same type of data.
A customer's name is text, the price is a number, and the payment status is either true or false.
To manage different kinds of information correctly, PHP uses Data Types.
What is a Data Type?
A Data Type defines the kind of value stored inside a variable.
It tells PHP how to store, process, and use that value.
Think of it like organizing items in your home.
- Clothes go into a wardrobe.
- Books go onto a bookshelf.
- Food goes into the refrigerator.
Similarly, PHP stores different kinds of data using different data types.
Why Do We Need Data Types?
Imagine an online banking application.
It stores information like:
- Customer Name
- Account Number
- Current Balance
- Is Account Active?
- Recent Transactions
Each piece of information is different.
If PHP treated everything as plain text, calculations, comparisons, and validations would become difficult.
Data types help PHP understand exactly how each value should be handled.
Common PHP Data Types
PHP supports several built-in data types. As a beginner, you'll use these most often.
| Data Type | Example | Used For |
|---|---|---|
| String | "Rahul" | Text |
| Integer | 100 | Whole numbers |
| Float | 99.95 | Decimal numbers |
| Boolean | true | True or False |
| Array | ["Apple","Orange"] | Multiple values |
| Object | new User() | Objects created from classes |
| NULL | null | No value assigned |
Don't worry if some of these look unfamiliar. We'll learn each one in detail in the upcoming lessons.
Example
<?php$name = "Rahul";$age = 28;$salary = 55000.75;$isActive = true;echo $name;echo "<br>";echo $age;echo "<br>";echo $salary;echo "<br>";echo $isActive;?>
Understanding the Example
$name = "Rahul";
This stores text, so it's a String.
$age = 28;
This stores a whole number, so it's an Integer.
$salary = 55000.75;
This contains a decimal value, so it's a Float.
$isActive = true;
This stores either true or false, so it's a Boolean.
Real-World Example
Let's create a simple employee profile.
<?php$employeeName = "Anita";$employeeId = 1054;$salary = 68000.50;$isPermanent = true;echo "Employee: " . $employeeName;echo "<br>";echo "ID: " . $employeeId;echo "<br>";echo "Salary: ₹" . $salary;echo "<br>";echo "Permanent Employee: " . ($isPermanent ? "Yes" : "No");?>
Output
Employee: Anita ID: 1054 Salary: ₹68000.5 Permanent Employee: Yes
Notice how each variable stores a different kind of information.
How to Check a Data Type
PHP provides a built-in function called gettype().
<?php$price = 2500;echo gettype($price);?>
Output
integer
This function is useful when you're debugging your applications.
Best Practices
- Choose the correct data type for every value.
- Use meaningful variable names.
- Avoid storing numbers as text unless necessary.
- Keep related data organized.
- Use Boolean values for true/false conditions.
Common Mistakes
Storing numbers as text
$price = "500";
Better
$price = 500;
Using unclear variable names
$x = true;
Better
$isLoggedIn = true;
Mixing unrelated values
Avoid storing different types of information in the same variable unless there's a valid reason.
Hands-on Exercise
Task: Create variables for Student Name, Age, Course Fee, and Is Fee Paid? Display all the values.
Challenge: Create variables for a movie booking system. Store Movie Name, Ticket Price, Number of Tickets, and Is Booking Confirmed? Display the information in a readable format.
<?php// TODO: complete the task$studentName = "Priya";$age = 22;// ...// Challenge: movie booking variables?>
Summary
- A data type defines the kind of value stored in a variable.
- PHP supports text, numbers, decimal values, Boolean values, arrays, objects, and null values.
- Choosing the correct data type makes your application easier to understand and maintain.
- Different data types are designed for different kinds of information.
- PHP automatically handles many data types, making development easier.
Key Takeaways
- Every variable stores a specific type of data.
- Strings store text.
- Integers store whole numbers.
- Floats store decimal values.
- Booleans store either true or false.
Interview Questions
Beginner
Q1What is a data type in PHP?
Q2Why are data types important?
Q3What is the difference between an Integer and a Float?
Q4Which data type stores text?
Q5Which data type stores true or false?
Intermediate
Q6How can you check the data type of a variable in PHP?
Q7Why shouldn't you store numeric values as strings unless required?
Q8What is the purpose of the NULL data type?
Advanced
Q9How does PHP determine a variable's data type automatically?
Q10How can choosing the correct data type improve application performance and readability?
What's Next?
Now that you understand what data types are, it's time to explore the most commonly used one in PHP.
In the next lesson, Constants, you'll learn how to define fixed values that never change — then we'll keep building on strings, numbers, and the rest of the type system throughout the course.