5. Variables and Data Types in PHP
Variables are temporary storage containers for data.
Declaring Variables
In PHP, all variables must start with a dollar sign ($). You do not need to explicitly declare the type (PHP handles this automatically).
php
Rules for Variable Names
- Must start with
$. - Must start with a letter (A-z) or an underscore (
_). - Cannot start with a number.
- Can only contain alphanumeric characters and underscores (
A-z, 0-9, _). - Are case-sensitive.
PHP Data Types
PHP supports several primitive data types:
- String: Text, enclosed in single or double quotes.
- Integer: Whole numbers (e.g., 10, -50).
- Float (or Double): Numbers with decimal points (e.g., 3.14, 1.5e-3).
- Boolean: Either
trueorfalse(case-insensitive in assignment). - Array: A collection of values (covered extensively later).
- Object: An instance of a class (covered in OOP).
- Resource: A special variable, often referencing external resources (like database connections).
- NULL: A variable with no value or that has been unset.