In PHP, a variable is a container used to store data, such as numbers, text strings, or arrays. Unlike many other languages, you don’t need to tell PHP what kind of data the variable will hold; it figures it out automatically.
 
 
Basic Example
 
<?php
  $greeting = "Hello world!"; // A string (text)
  $count = 5;                 // An integer (number)
  $price = 19.99;             // A float (decimal)

  echo $greeting;             // Outputs: Hello world!
?>


Types of PHP Variables
TypeDescriptionExample
StringA sequence of characters.$name = "John";
IntegerWhole numbers.$year = 2024;
FloatNumbers with a decimal point.$pi = 3.14;
BooleanTrue or False values.$is_admin = true;
ArrayStores multiple values in one variable.$colors = array("Red", "Blue");

 

Variable Scope (Where you can use them)
 
Where you declare a variable determines where you can access it:
  • Local: A variable declared inside a function can only be used inside that function.
  • Global: A variable declared outside a function. To use it inside a function, you must use the global keyword.
  • Static: A local variable that “remembers” its value even after the function has finished running.