Variables
A PHP variable is a $ followed by a name: $price, $userName, $_count. The name starts with a letter or underscore, then letters, digits and underscores, and it is case-sensitive. There is no declaration keyword — a variable is created the first time you assign to it.
PHP is dynamically typed: the variable has no type, the value does. The same variable can hold an int now and a string later. That is convenient in a short script and a source of bugs in a large one, which is why modern PHP adds type declarations on functions and properties (you will use them from Module 05).
<?php
$product = "Keyboard";
$price = 49;
$inStock = true;
echo "$product costs $price\n";
$price = $price + 10; // reassign using the old value
$price += 1; // the same idea, shorter
echo "New price: $price\n";
$price = "sold out"; // now it holds a string
echo "Price: $price\n";
[$a, $b] = [1, 2]; // assign two variables at once
[$a, $b] = [$b, $a]; // swap them
echo "a=$a b=$b\n";Keyboard costs 49
New price: 60
Price: sold out
a=2 b=1Add a variable $quantity = 3 and print the total cost of three keyboards at the original price.
Warning: Undefined variable — a typo PHP lets run
<?php
$userName = "Asha";
echo "Hello, $username!\n";Warning: Undefined variable $username in main.php on line 3
Hello, !Variable names are case-sensitive, so $username is a different, never-assigned variable. Reading it is only a warning: PHP treats it as null, prints an empty string, and keeps going. The script "works" and quietly prints the wrong thing — the most common beginner bug in PHP.
Use the exact name. Treat every warning as a bug: in development, run with all errors visible and fix each one. Static analysers like PHPStan (Module 11) catch this before you run.
<?php
$userName = "Asha";
echo "Hello, $userName!\n";if or loop still exists after it. Module 05 covers scope, global and closures.