Variables & Data Types
⏱ 12 min read read
Variables in PHP:
All PHP variables start with a dollar sign $. PHP is dynamically typed
--- you don't declare types, PHP figures them out automatically.
$age = 25; // int
$price = 9.99; // float
$isStudent = true; // bool
$name = "Alice"; // string
$nothing = null; // null
The 4 Core Scalar Types:
int --- whole numbers. PHP int size depends on platform (64-bit on
modern systems).
float --- decimal numbers (also called double).
bool --- true or false (case-insensitive: TRUE, False, etc. all work).
string --- text, in single or double quotes.
String Operations:
$name = "Alice";
strlen($name) // 5
strtoupper($name) // "ALICE"
strtolower($name) // "alice"
str_contains($name, "li") // true (PHP 8+)
substr($name, 1, 3) // "lic"
$name[0] // "A" (first char)
String Interpolation:
Double-quoted strings interpolate variables and escape sequences.
Single-quoted strings are literal --- no variable interpolation, no \n.
$name = "Alice";
$age = 25;
echo "Hello, $name! You are $age."; // interpolated
echo 'Hello, $name!'; // literal: Hello, $name!
echo "Hello, {$name}!"; // curly braces for clarity
IMPORTANT --- Type Juggling:
PHP automatically converts types in comparisons.
"1" == 1 → true (loose comparison, type juggling!)
"1" === 1 → false (strict comparison, type-safe)
ALWAYS use === for comparisons to avoid bugs.
PHP 8 made type juggling stricter but it still exists.
Constants:
Use define() or the const keyword. Constants have no $ prefix.
define('PI', 3.14159);
const MAX_STUDENTS = 30;
echo PI; // 3.14159
echo MAX_STUDENTS; // 30
All PHP variables start with a dollar sign $. PHP is dynamically typed
--- you don't declare types, PHP figures them out automatically.
$age = 25; // int
$price = 9.99; // float
$isStudent = true; // bool
$name = "Alice"; // string
$nothing = null; // null
The 4 Core Scalar Types:
int --- whole numbers. PHP int size depends on platform (64-bit on
modern systems).
float --- decimal numbers (also called double).
bool --- true or false (case-insensitive: TRUE, False, etc. all work).
string --- text, in single or double quotes.
String Operations:
$name = "Alice";
strlen($name) // 5
strtoupper($name) // "ALICE"
strtolower($name) // "alice"
str_contains($name, "li") // true (PHP 8+)
substr($name, 1, 3) // "lic"
$name[0] // "A" (first char)
String Interpolation:
Double-quoted strings interpolate variables and escape sequences.
Single-quoted strings are literal --- no variable interpolation, no \n.
$name = "Alice";
$age = 25;
echo "Hello, $name! You are $age."; // interpolated
echo 'Hello, $name!'; // literal: Hello, $name!
echo "Hello, {$name}!"; // curly braces for clarity
IMPORTANT --- Type Juggling:
PHP automatically converts types in comparisons.
"1" == 1 → true (loose comparison, type juggling!)
"1" === 1 → false (strict comparison, type-safe)
ALWAYS use === for comparisons to avoid bugs.
PHP 8 made type juggling stricter but it still exists.
Constants:
Use define() or the const keyword. Constants have no $ prefix.
define('PI', 3.14159);
const MAX_STUDENTS = 30;
echo PI; // 3.14159
echo MAX_STUDENTS; // 30
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress