Skip to Content
Course content

5: Variables and Data Types

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I want to start today with a piece of code I saw a junior dev struggle with last month. They were building a simple checkout system, and for some reason, the "VIP Discount" wasn't applying, even though the user definitely had a discount code. Here is the snippet that was causing the headache:

$discount_level = "10"; // This came from a database query
$required_level = 10;

if ($discount_level === $required_level) {
    echo "Discount applied!";
} else {
    echo "You do not qualify for this discount.";
}

The developer was scratching their head because, visually, 10 equals 10. But the code kept hitting the else block. I've seen this a hundred times—it's the classic "Type Juggling" trap in PHP.

The Strict Comparison Trap

The issue here is the === operator. In PHP, two equals signs (==) perform a loose comparison; PHP will try to convert the types to make them match. However, three equals signs perform a strict comparison. This means they check both the value and the data type.

In the example above, $discount_level is a string (notice the quotes), but $required_level is an integer. Even though they look the same to us, to PHP, a string is not an integer. The strict comparison fails, and the user loses their discount.

Casting to Ensure Consistency

The fix is to make sure you're comparing apples to apples. I usually prefer "casting" the variable to the type I expect. By putting (int) in front of the variable, we force PHP to treat that value as an integer.

$discount_level = "10";
$required_level = 10;

if ((int)$discount_level === $required_level) {
    echo "Discount applied!";
}

Now, (int)"10" becomes 10, the types match, and the logic works. It's a small change, but it saves you from those "ghost bugs" that are nearly impossible to find just by glancing at the code.

The PHP Type Toolkit

Since PHP is a dynamically typed language, you don't have to tell it that a variable is a string or an integer when you create it—it just figures it out based on the value you provide. But you still need to know what those types are to avoid the mess we just saw. Here are the ones you'll use 99% of the time:

  • Integers: Whole numbers (e.g., $count = 42;).
  • Floats: Decimal numbers, often called "doubles" (e.g., $price = 19.99;).
  • Strings: Text wrapped in quotes (e.g., $name = "Alice";). Pro tip: Use double quotes if you want to put a variable directly inside the string, like "Hello $name".
  • Booleans: Either true or false. These are the backbone of all your if statements.
  • Arrays: A single variable that holds multiple values (e.g., $colors = ["Red", "Green", "Blue"];). We'll dive deeper into these later, but just know they exist.
  • Null: A special type that represents a variable with no value. It's different from an empty string or a zero.

Variable Naming and the Dollar Sign

You already know that every variable in PHP starts with a $. It's a bit quirky if you're coming from JavaScript or Python, but you get used to it. One thing to keep in mind: variable names are case-sensitive. $user_name and $User_Name are two completely different boxes in your computer's memory. I highly recommend sticking to snake_case (all lowercase with underscores) for your variables; it's the most common convention in the PHP community and keeps your code readable.




📋 Practical Task

Building a Product Invoice Calculator

Your task is to create a small script that calculates the final price of an item after tax and a shipping fee. This will test your ability to handle different data types (strings, integers, and floats) and perform basic math.

Requirements:

  • Create a variable $product_name (String) and assign it a value (e.g., "Mechanical Keyboard").
  • Create a variable $unit_price (Float) and assign it a price (e.g., 89.99).
  • Create a variable $quantity (Integer) and assign it a number (e.g., 2).
  • Create a variable $tax_rate (Float) as a decimal (e.g., 0.07 for 7%).
  • Create a variable $shipping_fee (Float) (e.g., 5.50).

The Logic:

  1. Calculate the $subtotal (Price times Quantity).
  2. Calculate the $tax_amount (Subtotal times Tax Rate).
  3. Calculate the $final_total (Subtotal + Tax Amount + Shipping Fee).

The Output:
Display a simple summary using echo that looks something like this:
Item: Mechanical Keyboard | Quantity: 2 | Total: $198.47

Rating
0 0

There are no comments for now.

to be the first to leave a comment.