
Chapter 02: Variables, Data Types, and Constants
Overview
In the last chapter, we worked with static text like 'Hello, World!'. But to build dynamic applications, we need a way to store and manipulate information that can change. This is where variables come in. Variables are like containers that hold data, such as a username, the price of a product, or the result of a calculation.
In this chapter, you'll master the fundamentals of variables, explore PHP's different data types, and learn about constants—a special type of variable whose value cannot be changed. By the end, you'll be able to store, manipulate, and debug data effectively.
Prerequisites
- PHP installed: PHP 8.4 (check with
php --version) - Text editor: Any code editor (VS Code, Sublime Text, etc.)
- Terminal access: Command line/terminal to run PHP scripts
- Previous chapter completed: Chapter 01: Your First PHP Script
- Estimated time: 25–30 minutes
What You'll Build
By the end of this chapter, you'll have created several working PHP scripts that demonstrate:
- Variable declaration and usage
- String concatenation and interpolation
- Heredoc and Nowdoc syntax for multiline strings
- Complex string interpolation with curly braces
- Type inspection with
var_dump() - Explicit type casting and conversion
- Constant definition and usage in calculations
- A practical tax calculation script
Objectives
- Declare, assign, and use variables with proper naming conventions
- Understand PHP's primary data types: string, integer, float, and boolean
- Use
var_dump()to inspect the type and value of a variable - Combine and embed variables within strings using concatenation and interpolation
- Work with multiline strings using Heredoc and Nowdoc syntax
- Master complex string interpolation with curly braces
- Understand the difference between automatic type juggling and manual type casting
- Explicitly cast between types (
int,float,string,bool) - Define and use constants for values that should remain fixed
- Debug common variable-related errors
Quick Start
If you want to jump straight in, create a file called variables.php and run it:
# Create and run your first variable script
echo '<?php
$name = "Dale";
echo "Hello, $name!";
' > variables.php
php variables.php
# Expected output: Hello, Dale!Step 1: Understanding Variables (~3 min)
Goal
Create variables, assign values, and display them.
Think of a variable as a labeled box where you can store a piece of information. You give the box a name (the variable name) and put something inside it (the value).
Actions
- Create a new file:
# Create variables.php in your project directory
touch variables.php- Declare and use variables: Open
variables.phpand add:
# filename: variables.php
<?php
$name = 'Dale';
$age = 41;
echo $name;
echo $age;- Run the script:
# Execute the script
php variables.phpExpected Result
The output displays Dale41. It appears without spacing because echo prints values back-to-back unless you add whitespace.
Why It Works
- Variables start with
$followed by letters, numbers, or underscores (they’re case-sensitive). - The assignment operator (
=) stores the value on the right-hand side in the variable on the left. echooutputs the value currently stored in the variable.
Troubleshooting
- Parse error: unexpected '$name' — Make sure the file begins with
<?php. - **Undefined variable name
and$Name` as different variables. Match the case exactly. - Output missing spaces — Concatenate a space or use multiple
echostatements withPHP_EOLto separate lines.
Step 2: Working with Strings (~4 min)
Goal
Combine variables and text using concatenation and interpolation.
Strings are sequences of characters, like "Hello" or "PHP is fun!". PHP provides two primary ways to combine strings and variables: concatenation and interpolation.
Actions
- Demonstrate concatenation by updating
variables.php:
# filename: variables.php
<?php
$name = 'Dale';
$age = 41;
// The . operator joins strings together
echo 'My name is ' . $name . ' and I am ' . $age . ' years old.' . PHP_EOL;- Run the script:
php variables.php
# Expected output: My name is Dale and I am 41 years old.- Show interpolation (double quotes only) by replacing the previous
echowith:
# filename: variables.php
<?php
$name = 'Dale';
$age = 41;
// Double quotes enable variable interpolation
echo "My name is $name and I am $age years old." . PHP_EOL;- Verify the output again with
php variables.php.
Expected Result
Both approaches print the same sentence, demonstrating that concatenation and interpolation are interchangeable for simple cases.
Why It Works
- Concatenation (
.) glues separate strings and variables together before outputting. - Interpolation tells PHP to replace variable placeholders inside double quotes with their values.
- Single quotes treat everything literally, while double quotes interpret escape sequences (
\n,\t) and variables. PHP_EOLprints the correct end-of-line character for the current operating system.
Troubleshooting
- Output shows "$name" — Interpolation only works with double quotes.
syntax error, unexpected '.'— Quotes are mismatched or missing; ensure each string starts and ends with the same quote type.- Missing spaces — Add them manually (
'Hello' . ' ' . 'World') when concatenating.
Step 2b: Advanced String Syntax (~5 min)
Goal
Learn multiline strings and complex interpolation for more powerful text handling.
While single and double quotes work great for short strings, PHP offers additional syntax for working with longer, multiline text and complex variable interpolation.
Actions
- Create a Heredoc example:
# filename: heredoc.php
<?php
$name = "Dale";
$age = 41;
$role = "developer";
// Heredoc with interpolation (like double quotes)
$message = <<<EOT
Hello, my name is $name.
I am $age years old and I work as a $role.
This is perfect for:
- Multiline text
- Email templates
- HTML content
EOT;
echo $message . PHP_EOL;Run the script with
php heredoc.php.Add a Nowdoc example that treats everything literally (no interpolation):
# filename: heredoc.php
<?php
$name = "Dale";
// Nowdoc - no interpolation (like single quotes)
$template = <<<'EOT'
This is a literal string.
The variable $name will not be replaced.
Use this for code examples or templates.
EOT;
echo $template . PHP_EOL;- Create a complex interpolation example to illustrate curly braces:
# filename: interpolation.php
<?php
$user = [
'name' => 'Dale',
'age' => 41
];
$price = 29.99;
$quantity = 3;
// Complex interpolation requires braces
echo "User: {$user['name']}, Age: {$user['age']}" . PHP_EOL;
echo "Total: ${$price * $quantity}" . PHP_EOL; // Avoid this form in real code
echo "Total (cleaner): " . ($price * $quantity) . PHP_EOL; // Prefer concatenation for expressions- Execute the script:
php interpolation.phpExpected Result
Hello, my name is Dale.
I am 41 years old and I work as a developer.
This is perfect for:
- Multiline text
- Email templates
- HTML contentThis is a literal string.
The variable $name will not be replaced.
Use this for code examples or templates.User: Dale, Age: 41
Total: $89.97
Total (cleaner): 89.97Why It Works
- Heredoc (
<<<EOT) behaves like double quotes, so variables inside are interpolated and escape sequences are honored. - Nowdoc (
<<<'EOT') behaves like single quotes, leaving the contents untouched. - Curly braces (
{}) make complex interpolation unambiguous, especially when accessing arrays or object properties. - For arithmetic or concatenated expressions, standard concatenation often remains clearer and easier to debug.
Troubleshooting
syntax error, unexpected end of file— The closing identifier must be on its own line with no spaces or indentation.- Array interpolation fails — Wrap the array access in braces:
{$array['key']}. - Heredoc acts like Nowdoc — Ensure the identifier isn’t wrapped in single quotes if you want interpolation.
Step 3: Exploring Data Types (~5 min)
Goal
Understand PHP's primary data types and use var_dump() for debugging.
PHP is a "dynamically typed" language. This means you don't have to declare what type of data a variable will hold; PHP figures it out automatically at runtime. While modern PHP (8.0+) supports explicit type declarations for better code safety, understanding PHP's dynamic nature is essential.
Actions
- Create a type demonstration script named
types.php:
# filename: types.php
<?php
// Declaring variables with different types
$bookTitle = "The Lord of the Rings"; // string
$pageCount = 1216; // int
$price = 24.99; // float
$isPublished = true; // bool
echo "=== Variable Types ===" . PHP_EOL;
var_dump($bookTitle);
var_dump($pageCount);
var_dump($price);
var_dump($isPublished);
// Demonstrating type juggling
echo PHP_EOL . "=== Type Juggling ===" . PHP_EOL;
$stringNumber = "5";
$intNumber = 3;
$result = $stringNumber + $intNumber;
echo "\"5\" + 3 = ";
var_dump($result); // int(8) - PHP converts "5" to 5- Run the script:
php types.phpExpected Result
=== Variable Types ===
string(23) "The Lord of the Rings"
int(1216)
float(24.99)
bool(true)
=== Type Juggling ===
"5" + 3 = int(8)The number in parentheses after string indicates character count. For int and float, it shows the value itself. PHP automatically converts the string "5" to an integer when performing addition.
Why It Works
var_dump()reveals both the type and value of a variable, making it invaluable for debugging.- PHP's dynamic typing performs implicit type conversions (type juggling) when needed.
- Other helpful introspection tools include
print_r(),var_export(), andgettype().
Tip: Use
var_dump()liberally when debugging dynamic values—it provides a complete snapshot of type and content.
Troubleshooting
- Boolean shows as
bool(false)with no output —echoprints nothing forfalse, butvar_dump()confirms the value. - Numbers appear as strings — Remove the quotes if you intend to store numeric values.
- Unexpected math results — Inspect each operand with
var_dump()to ensure types are what you expect; use===for strict comparisons.
Step 3b: Type Casting and Conversion (~4 min)
Goal
Learn to explicitly control type conversion instead of relying on automatic type juggling.
While PHP's automatic type juggling is convenient, sometimes you need precise control over type conversion. This is where type casting comes in—manually converting a value from one type to another.
Actions
- Create a type casting demonstration in
casting.php:
# filename: casting.php
<?php
echo "=== Type Casting Examples ===" . PHP_EOL . PHP_EOL;
// String to Integer
$stringNumber = "42";
$intNumber = (int)$stringNumber;
echo "String to Int:" . PHP_EOL;
var_dump($stringNumber); // string(2) "42"
var_dump($intNumber); // int(42)
echo PHP_EOL;
// Float to Integer (truncates decimal)
$floatPrice = 19.99;
$intPrice = (int)$floatPrice;
echo "Float to Int (truncates):" . PHP_EOL;
var_dump($floatPrice); // float(19.99)
var_dump($intPrice); // int(19)
echo PHP_EOL;
// Integer to String
$age = 41;
$ageString = (string)$age;
echo "Int to String:" . PHP_EOL;
var_dump($age); // int(41)
var_dump($ageString); // string(2) "41"
echo PHP_EOL;
// Boolean Conversions
echo "=== Boolean Conversions ===" . PHP_EOL;
var_dump((bool)""); // bool(false) - empty string
var_dump((bool)"0"); // bool(false) - string "0"
var_dump((bool)"text"); // bool(true) - non-empty string
var_dump((bool)0); // bool(false) - zero
var_dump((bool)1); // bool(true) - non-zero
var_dump((bool)-1); // bool(true) - non-zero
echo PHP_EOL;
// Practical Example: User Input
$userInput = "123";
$productId = (int)$userInput; // Ensure it's an integer
echo "Processing product ID: ";
var_dump($productId); // int(123)- Run the script:
php casting.phpExpected Result
=== Type Casting Examples ===
String to Int:
string(2) "42"
int(42)
Float to Int (truncates):
float(19.99)
int(19)
Int to String:
int(41)
string(2) "41"
=== Boolean Conversions ===
bool(false)
bool(false)
bool(true)
bool(false)
bool(true)
bool(true)
Processing product ID: int(123)Why It Works
- Casting syntax
(type)$valueforces PHP to convert values immediately, preventing unexpected type juggling. - When coercing to boolean, remember the "falsy" values:
0,0.0,"0","",false,null, and[]. - Explicit casts like
(int),(float),(string),(bool), and(array)make your intentions clear to readers and linters.
Practical Use Cases
// Sanitizing user input
$userId = (int)$_GET['id'];
// Formatting output
$price = 29.99;
$displayPrice = "$" . (string)$price;
// Boolean checks
$hasAccess = (bool)$userPermissions;
// Preventing decimal issues
$quantity = (int)$floatQuantity;Best Practice: Use type casting when accepting external input (forms, URLs, APIs) to ensure data is the expected type before processing.
Troubleshooting
- Unexpected
1instead oftrue— Casting a non-zero number to boolean yieldstrue, which prints as1withecho. Usevar_dump()for clear output. - Float casting seems lossy — Casting a float to int truncates decimals. Use
round()ornumber_format()when you need rounding instead of truncation. "123abc"becomes123—(int)reads numeric characters until it encounters something non-numeric. Validate strings before casting if you require strict numeric input.
Step 4: Using Constants (~3 min)
Goal
Define and use constants for values that should never change.
Sometimes you need to use a value that should never change during the execution of your script, like a configuration setting or a mathematical value like PI. For this, you use constants.
Actions
- Create a tax calculator in
constants.php:
# filename: constants.php
<?php
// Define a constant for the sales tax rate
const TAX_RATE = 0.08;
$productPrice = 150;
$totalPrice = $productPrice + ($productPrice * TAX_RATE);
echo "Product price: $" . $productPrice . PHP_EOL;
echo "Tax rate: " . (TAX_RATE * 100) . "%" . PHP_EOL;
echo "Total price: $" . $totalPrice . PHP_EOL;- Run the calculator:
php constants.phpExpected Result
Product price: $150
Tax rate: 8%
Total price: $162Why It Works
- Constants are defined once and cannot be changed. They:
- Do not use the
$prefix - Cannot be redefined or undefined once set
- Are typically written in
UPPER_SNAKE_CASE - Have global scope (accessible everywhere without passing them around)
- Do not use the
constis preferred overdefine()because it’s resolved at compile time, has cleaner syntax, and works inside classes. Usedefine()only when you need dynamic names or conditional definitions.- Attempting to change a constant triggers a fatal error, protecting critical configuration values from accidental modification.
Troubleshooting
- "Undefined constant TAX_RATE" — Constants don’t use
$. WriteTAX_RATE, not$TAX_RATE. - "Constant TAX_RATE already defined" — You declared it twice. Remove duplicate definitions.
- "Cannot redeclare constant TAX_RATE" — Constants are immutable. Use a variable if you need a changeable value.
Exercises
Practice what you've learned with these hands-on exercises:
Personal Profile (~5 min): Create a script called
profile.phpthat stores your first name, last name, and year of birth in variables. Then, print out a complete sentence using interpolation.Hints:
- Use
camelCasefor variable names (e.g.,$firstName,$lastName) - Use double quotes for string interpolation
- Add
PHP_EOLat the end for a clean newline
Expected output:
textMy name is Dale Hurley. I was born in 1990.- Use
Circle Calculator (~8 min): Create a script called
circle.phpthat calculates and prints the circumference and area of a circle.Requirements:
- Store the radius in a variable (e.g.,
$radius = 5;) - Define a constant for PI:
const PI = 3.14159; - Calculate the circumference:
2 * PI * radius - Calculate the area:
PI * radius * radius(or use$radius ** 2for squaring) - Print the results in a human-readable format
- Use
var_dump()to verify the data types of your calculated values
Hints:
- Use descriptive variable names like
$circumferenceand$area - The
**operator performs exponentiation (power), so$radius ** 2is the same as$radius * $radius - All calculations should result in
floattypes
Expected output:
textCircle with radius: 5 Circumference: 31.4159 Area: 78.53975- Store the radius in a variable (e.g.,
Temperature Converter (~8 min): Create a script called
temperature.phpthat converts Celsius to Fahrenheit.Requirements:
- Store a temperature in Celsius as a variable (e.g.,
$celsius = 25;) - Use the formula:
fahrenheit = (celsius * 9/5) + 32 - Print both temperatures with descriptive text
- Use
var_dump()to check the types of both values
Hints:
- The division
9/5produces afloat(1.8), making your result afloat - Use parentheses to ensure correct order of operations
- Consider using
number_format()to control decimal places in output (e.g.,number_format($fahrenheit, 1)for one decimal place)
Expected output:
textTemperature in Celsius: 25 Temperature in Fahrenheit: 77- Store a temperature in Celsius as a variable (e.g.,
Type Casting Exercise (~8 min): Create a script called
type-casting.phpthat demonstrates safe type casting for user input simulation.Requirements:
- Simulate user input with string variables:
$inputAge = '25',$inputPrice = '19.99',$inputQuantity = '3.7' - Cast each input to the appropriate type
- Calculate total price (price × quantity, with quantity as integer)
- Check if age is valid for adult content (age >= 18)
- Use
var_dump()to show types before and after casting - Display a summary message
Hints:
- Cast
$inputAgetointfor comparison - Cast
$inputPricetofloatfor calculation - Cast
$inputQuantitytoint(truncates to 3) - Use boolean casting to verify access
Expected output:
text=== Before Casting === string(2) "25" string(5) "19.99" string(3) "3.7" === After Casting === int(25) float(19.99) int(3) Age verification: Adult access granted Order: 3 items at $19.99 each Total: $59.97- Simulate user input with string variables:
Bonus: Shopping Cart (~10 min): Create a script called
cart.phpthat calculates the total cost of items in a shopping cart with tax.Requirements:
- Create variables for at least 3 product prices (e.g.,
$item1 = 29.99;) - Define a constant for the tax rate (e.g.,
const TAX_RATE = 0.07;) - Calculate the subtotal (sum of all items)
- Calculate the tax amount (
subtotal * TAX_RATE) - Calculate the final total (
subtotal + tax) - Print a receipt showing each item, subtotal, tax, and total
- Use
number_format($price, 2)to format prices to 2 decimal places
Expected output:
textItem 1: $29.99 Item 2: $15.50 Item 3: $8.75 ───────────────── Subtotal: $54.24 Tax (7%): $3.80 ───────────────── Total: $58.04- Create variables for at least 3 product prices (e.g.,
Super Bonus: Email Template with Heredoc (~12 min): Create a script called
email.phpthat generates a formatted email using Heredoc syntax.Requirements:
- Use variables for:
$userName,$orderId,$orderTotal,$itemCount - Create an email body using Heredoc that includes:
- A personalized greeting
- Order confirmation details
- A thank you message
- Footer with company info
- Use proper formatting with line breaks
Expected output:
textDear Dale, Thank you for your order! Order Details: Order ID: #12345 Items: 3 Total: $89.97 Your order will be processed within 24 hours. Best regards, The PHP Store Team- Use variables for:
Wrap-up
Excellent work! You now have a solid understanding of how to store and manage data in PHP. Let's recap what you've learned:
What You've Accomplished
- ✓ Declared and used variables with proper naming conventions (
camelCase) - ✓ Mastered string concatenation and interpolation (single vs. double quotes)
- ✓ Learned Heredoc and Nowdoc syntax for multiline strings
- ✓ Implemented complex string interpolation with curly braces
- ✓ Understood PHP's primary scalar data types and automatic type juggling
- ✓ Mastered explicit type casting for precise control over data types
- ✓ Learned PHP's falsy values and boolean conversion rules
- ✓ Used
var_dump()and other debugging functions to inspect types - ✓ Defined and used constants with
constfor immutable values - ✓ Built practical scripts including a tax calculator
- ✓ Learned when to use
constvsdefine()
Key Takeaways
- Variables use the
$prefix and followcamelCasenaming convention; constants useUPPER_SNAKE_CASEwithout$ - Double quotes enable interpolation and escape sequences; single quotes are literal
- Use Heredoc (
<<<EOT) for multiline strings with interpolation; use Nowdoc (<<<'EOT') for literal multiline text - Complex interpolation (arrays, expressions) requires curly braces:
{$array['key']} var_dump()is essential for debugging—it shows both type and value- PHP is dynamically typed and performs automatic type juggling (implicit conversion)
- Type casting gives you explicit control:
(int)$value,(string)$value,(bool)$value - Know your falsy values:
0,0.0,"","0",null,false,[] - Use
constfor defining constants (preferred overdefine()) - Constants have global scope and prevent accidental value changes
- Type casting is crucial when handling external input for security and correctness
These concepts are the absolute bedrock of programming in any language. Every application you build will rely heavily on variables, data types, and constants to manage state and perform calculations.
Next Steps
In Chapter 03: Control Structures, you'll learn how to make decisions and repeat actions in your code using if statements and loops—bringing your scripts to life with logic and automation.
Code Examples
Complete, runnable examples from this chapter are available in:
data-types-demo.php- Working with different data typesconstants-demo.php- Defining and using constantstype-juggling.php- Understanding type juggling and conversionstrict-types-demo.php- Strict type mode demonstrationsolutions/- Solutions to chapter exercises
Further Reading
- PHP Manual: Variables — Official PHP documentation on variables
- PHP Manual: Types — Complete guide to PHP data types including type juggling
- PHP Manual: Constants — Deep dive into constants and their usage
- PHP 8.4 Release Notes — What's new in PHP 8.4
- Type Declarations — Learn about strict typing in modern PHP
Knowledge Check
Test your understanding of variables, data types, and constants: