What is a Variable in PHP?
A variable in PHP is a storage location identified by a name (or identifier) that holds data which can be modified during the execution of a script. In PHP, a variable name starts with a dollar sign ($) followed by the name of the variable.
Table of Contents
ToggleDeclaring Variables:
- Variables in PHP are declared by simply assigning a value to them using the assignment operator (=).
 - PHP automatically determines the variable type based on the value it is assigned.
 
Example:
$age = 25;           // Integer
$name = "John";      // String
$is_admin = true;    // Boolean
$price = 99.99;      // Float
Types of Variables in PHP:
1. Integer
- Description: An integer is a non-decimal number between -2,147,483,648 and 2,147,483,647.
 - Example:
 
  $number = 42;
  echo $number; // Output: 42
2. Float (Floating Point Number)
- Description: A float is a number with a decimal point or a number in exponential form.
 - Example:
 
  $price = 19.99;
  echo $price; // Output: 19.99
3. String
- Description: A string is a sequence of characters surrounded by quotes.
 - Example:
 
  $greeting = "Hello, World!";
  echo $greeting; // Output: Hello, World!
4. Boolean
- Description: A boolean represents two possible states: true or false.
 - Example:
 
  $is_active = true;
  $is_inactive = false;
  echo $is_active; // Output: 1 (true)
5. Array
- Description: An array is a variable that can hold multiple values. Arrays can be indexed or associative.
 - Example:
 
  $colors = ["red", "green", "blue"]; // Indexed array
  $person = ["name" => "John", "age" => 30]; // Associative array
  print_r($colors);
  print_r($person);
6. Object
- Description: An object is an instance of a class. Objects are used to access properties and methods of a class.
 - Example:
 
  class Car {
      public $make;
      public $model;
      public function __construct($make, $model) {
          $this->make = $make;
          $this->model = $model;
      }
      public function getDetails() {
          return $this->make . " " . $this->model;
      }
  }
  $myCar = new Car("Toyota", "Corolla");
  echo $myCar->getDetails(); // Output: Toyota Corolla
7. NULL
- Description: A variable with no value assigned is considered NULL.
 - Example:
 
  $var = NULL;
  echo $var; // Output: (nothing is displayed)
8. Resource
- Description: A resource is a special variable that holds a reference to an external resource, such as a database connection.
 - Example:
 
  $file = fopen("test.txt", "r");
  // $file is a resource
Summary:
- Integers: Whole numbers without a decimal point.
 - Floats: Numbers with a decimal point or in exponential form.
 - Strings: Sequences of characters.
 - Booleans: True or false values.
 - Arrays: Collections of values that can be indexed or associative.
 - Objects: Instances of classes.
 - NULL: Represents a variable with no value.
 - Resources: Holds references to external resources.
 
These examples cover the basic types of variables in PHP and how they are used in a script.
											