Back to course

Variables and Data Types in PHP

PHP: The Complete 0 to Hero Bootcamp

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

  1. Must start with $.
  2. Must start with a letter (A-z) or an underscore (_).
  3. Cannot start with a number.
  4. Can only contain alphanumeric characters and underscores (A-z, 0-9, _).
  5. Are case-sensitive.

PHP Data Types

PHP supports several primitive data types:

  1. String: Text, enclosed in single or double quotes.
  2. Integer: Whole numbers (e.g., 10, -50).
  3. Float (or Double): Numbers with decimal points (e.g., 3.14, 1.5e-3).
  4. Boolean: Either true or false (case-insensitive in assignment).
  5. Array: A collection of values (covered extensively later).
  6. Object: An instance of a class (covered in OOP).
  7. Resource: A special variable, often referencing external resources (like database connections).
  8. NULL: A variable with no value or that has been unset.