PHP Mastery Tutorial 0/120 lessons ~6 min read Lesson 7

    Data Types

    Learn PHP data types — strings, integers, floats, booleans, arrays, objects, and null — and why choosing the right type matters for every variable.

    Course progress0%
    Focus
    15 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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 TypeExampleUsed For
    String"Rahul"Text
    Integer100Whole numbers
    Float99.95Decimal numbers
    BooleantrueTrue or False
    Array["Apple","Orange"]Multiple values
    Objectnew User()Objects created from classes
    NULLnullNo value assigned

    Don't worry if some of these look unfamiliar. We'll learn each one in detail in the upcoming lessons.

    Example

    php
    <?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

    php
    $name = "Rahul";

    This stores text, so it's a String.

    php
    $age = 28;

    This stores a whole number, so it's an Integer.

    php
    $salary = 55000.75;

    This contains a decimal value, so it's a Float.

    php
    $isActive = true;

    This stores either true or false, so it's a Boolean.

    Real-World Example

    Let's create a simple employee profile.

    php
    <?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
    <?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

    php
    $price = "500";

    Better

    php
    $price = 500;

    Using unclear variable names

    php
    $x = true;

    Better

    php
    $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
    <?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?
    A data type defines the kind of value a variable holds — how PHP stores, processes, and interprets that value (string, integer, float, boolean, etc.).
    Q2Why are data types important?
    They let PHP perform correct operations — math on numbers, concatenation on strings, comparisons on booleans — and prevent bugs from treating unlike values the same way.
    Q3What is the difference between an Integer and a Float?
    An integer is a whole number without a decimal (28, 100). A float (or double) includes a decimal point (99.95, 55000.75) for fractional values.
    Q4Which data type stores text?
    String — text enclosed in single or double quotes, e.g. $name = "Rahul";
    Q5Which data type stores true or false?
    Boolean — only two values: true or false, used for flags like $isActive = true;

    Intermediate

    Q6How can you check the data type of a variable in PHP?
    Use gettype($var) for the type name, var_dump($var) for type and value, or is_string(), is_int(), is_float(), is_bool() for specific checks.
    Q7Why shouldn't you store numeric values as strings unless required?
    Strings disable proper math — "100" + "50" may concatenate to "10050" instead of 150. Store numbers as int/float for calculations and comparisons.
    Q8What is the purpose of the NULL data type?
    NULL represents a variable with no value assigned — explicitly empty or unset. It differs from 0, false, or "" which are actual values.

    Advanced

    Q9How does PHP determine a variable's data type automatically?
    PHP is dynamically typed — the type is inferred from the assigned value at runtime. $x = 5 becomes integer; $x = "hi" becomes string when reassigned.
    Q10How can choosing the correct data type improve application performance and readability?
    Correct types avoid coercion overhead, enable strict comparisons (===), make intent clear to readers, and let static analysis and IDEs catch errors earlier.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.