Variables and Data Types
25 minPHP is a loosely typed language, meaning you don't need to declare variable types explicitly. PHP automatically determines types based on the assigned value. However, modern PHP (7.0+) supports type declarations (type hints) for function parameters and return values, enabling better code quality and performance. Understanding when to use type hints helps write more robust PHP code.
Variables in PHP start with a dollar sign ($) and can store different types of data. Variable names are case-sensitive and must start with a letter or underscore, followed by letters, numbers, or underscores. PHP variables don't need to be declared before use, but it's good practice to initialize them. Understanding variable naming and scope is essential for writing maintainable PHP code.
PHP supports scalar types (int, float, string, bool), compound types (array, object), and special types (null, resource). Scalar types hold single values, while compound types can hold multiple values or complex data structures. PHP 7.0+ added scalar type declarations, allowing you to specify expected types for better code safety. Understanding PHP's type system helps prevent bugs and write clearer code.
Type juggling in PHP automatically converts between types when needed. For example, PHP converts strings to numbers in arithmetic operations. While convenient, this can lead to unexpected behavior if not understood. Type coercion rules determine how conversions happen. Understanding type juggling helps you write predictable code and avoid subtle bugs.
PHP arrays are ordered maps that can act as arrays, lists, hash tables, dictionaries, and more. Arrays can have numeric or string keys (or both), and values can be of any type. PHP 7.4+ introduced typed properties, and PHP 8.0+ added union types and other advanced type features. Understanding arrays and their flexibility is crucial for PHP development.
Variable scope in PHP determines where variables can be accessed. Global variables are accessible throughout a script, local variables exist only within functions, and static variables persist across function calls. The `global` keyword and `$GLOBALS` array allow access to global variables from functions. Understanding scope prevents bugs and helps design better code structure.
Key Concepts
- PHP is loosely typed but supports type hints (PHP 7.0+).
- Variables start with $ and don't need explicit type declaration.
- PHP supports scalar, compound, and special types.
- Type juggling automatically converts between compatible types.
- Arrays are flexible data structures supporting multiple use cases.
Learning Objectives
Master
- Declaring and using variables in PHP
- Understanding PHP's type system and type juggling
- Working with arrays and their various forms
- Using type hints for better code quality
Develop
- Understanding dynamic typing and its implications
- Writing type-safe PHP code with type hints
- Designing code with proper variable scope
Tips
- Use type hints in function parameters: function add(int $a, int $b): int.
- Initialize variables before use to avoid undefined variable warnings.
- Use strict comparison (===) instead of loose (==) to avoid type juggling surprises.
- Use array() or [] syntax for arrays ([] is shorter and preferred).
Common Pitfalls
- Not using type hints, missing opportunities for better code quality.
- Using == instead of ===, causing unexpected type coercion.
- Not understanding variable scope, causing undefined variable errors.
- Assuming variables are initialized, causing undefined variable warnings.
Summary
- PHP is loosely typed but supports type hints for better code quality.
- Variables start with $ and types are determined automatically.
- Type juggling converts between types automatically.
- Understanding PHP's type system helps write robust code.
Exercise
Demonstrate different data types and variable usage in PHP.
<?php
// Scalar types
$name = "John Doe";
$age = 25;
$height = 1.75;
$isStudent = true;
// Array
$hobbies = ["reading", "coding", "hiking"];
// Associative array
$person = [
"name" => "Alice",
"age" => 30,
"city" => "New York"
];
// Display variables
echo "Name: " . $name . "<br>";
echo "Age: " . $age . "<br>";
echo "Height: " . $height . "m<br>";
echo "Is Student: " . ($isStudent ? "Yes" : "No") . "<br>";
echo "Hobbies: " . implode(", ", $hobbies) . "<br>";
echo "Person: " . $person["name"] . " from " . $person["city"] . "<br>";
// Type checking
echo "Name type: " . gettype($name) . "<br>";
echo "Age type: " . gettype($age) . "<br>";
?>