PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
5: Variables and Data Types
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
trueorfalse. These are the backbone of all yourifstatements. - 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:
- Calculate the
$subtotal(Price times Quantity). - Calculate the
$tax_amount(Subtotal times Tax Rate). - 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
There are no comments for now.