MasterPHP.in
PHP Tutorial

PHP Data Types


Introduction

Now that you know what PHP is, the next step is understanding data types. Data types define what kind of value a variable can store — things like text, numbers, or lists. Every PHP program uses them, so getting comfortable with them early makes everything else easier.


What Are Data Types in PHP?

Think of a data type as a label that tells PHP what kind of information you're working with. When you create a variable and assign a value to it, PHP looks at that value and figures out what type it is.

Here's a simple example:

<?php
$name = "John";   // String
$age = 25;        // Integer
?>

In the first line, PHP sees the quotes and knows $name holds text. In the second line, it sees a plain number and knows $age holds an integer. You don't have to tell PHP yourself — it figures it out on its own.


The Main Data Types in PHP

PHP has several data types, but as a beginner you really only need to focus on these seven. Let's walk through each one.

1. String

A string is simply text. Anything you wrap in quotes — single or double — is a string.

<?php
$name = "Rohan";
echo $name;
?>

Output:

Rohan

Strings are the most common data type you'll use, especially when displaying content on a page or storing user input.

2. Integer

An integer is a whole number — no decimals, no fractions.

<?php
$age = 25;
echo $age;
?>

Integers can be positive or negative. You'd use them for things like counting items, storing ages, or tracking scores.

3. Float (Decimal Number)

A float is a number that has a decimal point. You'll see it called a "float" or sometimes a "double."

<?php
$price = 99.99;
echo $price;
?>

Use floats whenever precision matters — prices, measurements, percentages, and so on.

4. Boolean

A boolean is the simplest data type — it can only be one of two things: true or false. That's it.

<?php
$is_logged_in = true;
?>

Booleans are incredibly useful for controlling the flow of your program. For example, you might check whether a user is logged in before showing them a dashboard.

5. Array

An array lets you store multiple values inside a single variable. Instead of creating ten separate variables for ten items, you can group them all together.

<?php
$colors = ["red", "green", "blue"];
echo $colors[0];
?>

Output:

red

Notice the [0] — arrays in PHP start counting from zero, not one. So the first item is at index 0, the second at 1, and so on. This trips up a lot of beginners at first, but you'll get used to it quickly.

6. Object

An object is a more advanced concept built on something called a "class." For now, just think of a class as a blueprint, and an object as something you build from that blueprint.

<?php
class Car {
  public $name = "BMW";
}
$car = new Car();
echo $car->name;
?>

You don't need to fully understand objects yet — they become important when you get into object-oriented programming. For now, it's enough to know they exist.

7. NULL

NULL simply means a variable has no value at all. It's not zero, it's not an empty string — it's nothing.

<?php
$x = NULL;
?>

You might use NULL to intentionally empty out a variable, or you'll encounter it when a variable hasn't been assigned anything yet.


How to Check a Variable's Data Type

If you ever want to peek under the hood and see exactly what type a variable is (and what it contains), use var_dump().

<?php
$x = "Hello";
var_dump($x);
?>

Output:

string(5) "Hello"

This tells you the type is string and the length is 5 characters. It's a really handy tool when you're debugging and something isn't behaving the way you expect.


PHP Is Loosely Typed

One thing that makes PHP different from some other languages is that you never have to declare a variable's type yourself. PHP figures it out automatically based on the value you assign.

<?php
$x = 10;
$x = "Hello"; // This is perfectly fine in PHP
?>

You can even reassign a variable to a completely different type, and PHP won't complain. This is called being "loosely typed." It makes PHP flexible and beginner-friendly, though it can occasionally cause unexpected behavior if you're not careful.


Common Mistakes to Watch Out For

As you start working with data types, a few mistakes come up again and again:

  • Mixing numbers and strings incorrectly. For example, trying to do math on a variable that holds text won't give you the result you expect.
  • Forgetting quotes around strings. If you write $name = John; without quotes, PHP will think John is a constant or function name, not a word.
  • Using the wrong array index. Remember, arrays start at 0. If you try to access $colors[1] thinking it's the first item, you'll actually get the second one.

These are small things, but catching them early saves a lot of head-scratching later.


Pro Tip: Use Meaningful Variable Names

This isn't directly about data types, but it's worth mentioning now while you're building good habits. Always name your variables in a way that describes what they hold.

$user_name = "John"; // Good — tells you exactly what this stores
$x = "John";         // Bad — tells you nothing

When your code grows to hundreds of lines, clear variable names are the difference between code you can read and code that confuses even you.


Quick Reference

Here's a summary of all seven data types covered in this lesson:

Data TypeWhat It StoresExampleStringText "Hello"I ntegerWhole numbers 25 FloatDecimal numbers 99.99 BooleanTrue or false true ArrayMultiple values ["red", "green"] ObjectInstance of a class new Car() NULLNo value NULL


Conclusion

You now have a solid understanding of the seven basic PHP data types. These are the building blocks that every PHP program is made of — once you're comfortable with them, variables and logic will start to feel much more natural.


Try it online

Share this tutorial