Variables are dynamically-typed because the type of the variable is linked to the value of the variable. You could define a variable for a string, store a string, and then replace the string with a number. To do the same thing in C++, you would have to cast, or change the type of, the variable, and store it in a different "container".
Note: Variables are the basis of any programming language: they are "containers" (spaces in memory) which hold data. The data can change, thus it is "variable".
- All variables in PHP are denoted with a leading dollar sign ($) like : $var_name.
- These identifiers are case-sensitive, meaning that capitalization matters, so $easy is different from $Easy.
- The value of a variable is the value of its most recent assignment.
- Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
- Variables can, but do not need, to be declared before assignment.
- A variable does not know in advance whether it will be used to store a number or a string of characters.
- Variables used before they are assigned have default values.
- PHP does a good job of automatically converting types from one to another when necessary.
Variable Scope:
Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types:
- Local variables
- Function parameters
- Global variables
- Static variables
Variable Naming:
Rules for naming a variable is:
- Variable names must begin with a letter or underscore character.
- A variable name can consist of numbers, letters, underscores but you cannot use characters like, etc
There is no size limit for variables.