A variable is a named memory area that can hold values or expression.

  • We can consider a variable as a container that stores value.
  • PHP variables are used to hold values or expressions.
  • We can refer a variable by its name.
  • A variable should have descriptive and meaningful name, like amount, first_name, balance, joining_date etc.
  • All PHP variables should have to follow variable naming convention rules.

Naming Convention Rules for PHP Variables

  • PHP Variables starts with a ‘$’ sign, followed by a name of the variable
  • First letter must be alphabet or the underscore character (‘_’)
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and ‘_’ )
  • It should not contain white spaces
  • Variable names are case sensitive (means “name” and “NAME” are two different variables)

PHP Variables Declaration

  • PHP does not use any command for declaring a variable.
  • A variable is declared at the time we first assign a value to it.
  • For example:
$first_name = "Same";
  • After the execution of the above declaration statement, the variable first_name will hold the value Same.
  • Note that while assigning a string or text value to a variable, we have to surround it by quotes.
  • If you are in need to declare an empty variable, you can assign null value to it.
  • For example :
$item_name = null

PHP is a Loosely Typed Language

  • In PHP, we need not to specify a data type at the time of variable declaration.
  • We do not have to tell PHP of what data type the variable is.
  • PHP automatically converts the variable to the correct data type, depending on the value we assign to it.
  • In a strongly typed programming language such as C, C++, Java, we have to specify both the data type and name of the variable before using it.