Chapter 2: Variables & Data Types

Learning Objectives

By the end of this chapter, you will be able to:

  • Explain the concept and importance of variables in programming.
  • Correctly declare and assign values to variables in PHP.
  • Differentiate between the primary data types: String, Integer, Float, and Boolean.
  • Display the value of variables and combine them with text using concatenation.

Introduction: The Magic Information Box

In the last chapter, we learned that PHP is the "chef" that cooks our website. But a chef can't cook without "ingredients." In programming, our ingredients are data.

To work with data, we need a place to store it. In the programming world, these containers are called Variables.

A variable is simply a "box" that we give a name to, where we can store a piece of information. The information inside this box can be changed at any time.

2.1 Variables in PHP

Variables in PHP are very easy to spot because they always begin with a dollar sign ($).

The Basic Syntax:

$variableName = value;
  • $variableName: The name of our variable (the label on the box).
  • =: The assignment operator, which means "store the value on the right inside the variable on the left."
  • value: The data we want to store.
  • ;: The semicolon, which marks the end of a statement.

Example:

<?php
    $productName = "Cool T-Shirt"; // Creates a box named productName and puts the text "Cool T-Shirt" inside.
    $quantity = 10; // Creates a box named quantity and puts the number 10 inside.
?>

Important Rules for Naming Variables:

  • Must start with a $ sign, followed by a letter or an underscore (_).
  • Can only contain letters, numbers, and underscores.
  • Case-Sensitive: $name and $Name are two different variables.
  • Best Practice: Use "camelCase" for readability (e.g., $customerFirstName).

2.2 Types of Ingredients: Basic Data Types

PHP is a Loosely Typed language, meaning you don't have to tell it what type of data a variable will hold. PHP figures it out automatically. However, understanding the different data types is essential.

Here are the most common ones:

  • String (Text)
    A sequence of characters, enclosed in either " (double quotes) or ' (single quotes).
    $customerName = "John Doe";
  • Integer (Whole Numbers)
    Any whole number, without decimals.
    $orderAmount = 5;
  • Float / Double (Decimal Numbers)
    A number with a decimal point.
    $price = 499.99;
  • Boolean (True/False)
    Represents only two possible states: true or false. It's like an "on/off" switch, used for logical conditions.
    $isMember = true;
  • NULL (No Value)
    A special data type that simply means "has no value."
    $discountCode = null;

2.3 Using Your Data: Displaying Variables

We can use the echo command from Chapter 1 to display the data stored inside a variable.

<?php
    $message = "Welcome to our shop!";
    echo $message; // Outputs: Welcome to our shop!
?>

Combining Text and Variables (Concatenation)

Most of the time, you'll want to combine static text with the value of a variable. We do this by "concatenating" them using the dot (.) operator.

Example:

<?php
    $product = "Laptop";
    $price = 25000;

    // We use the dot operator to join everything together
    echo "<p>The price of the " . $product . " is " . $price . " baht.</p>"; 
    // Outputs: The price of the Laptop is 25000 baht.
?>

Pro Tip: If you use double quotes (") to create your string, PHP can automatically insert the value of a variable for you. This is called Variable Interpolation.

<?php
    $customer = "Jane";
    // This only works with double quotes!
    echo "<p>Hello, $customer. Welcome back!</p>";
    // Outputs: Hello, Jane. Welcome back!
?>

Conclusion & What's Next

In this chapter, you learned how to create "boxes" (variables) to store your "ingredients" (data) in various formats. You can now store and display information dynamically.

Now that we have our ingredients ready, the next step is to learn how to cook with them. In the next chapter, we will explore "Operators" to perform calculations, comparisons, and manipulate our data.