Php – Variables
Variables are used to store data in application. Variables can store data like string, text, integer, float number, logic value etc. Variables may defined different ways but there are some restrictions for variables names. Here some important things about variable naming.
All variables start with
$ sign. It is like variable selector. Following example show valid and invalid variable named.
Variable Name Must Start with Letter or Underscore
Variable names can not start other than letter or underscore. If this is not obeyed will create fatal error.
Variable Name Cannot Start with Number
Variables can not start with numbers. This is one of the pitfalls new developers or learner fall.
Variable Name Contain Alphanumeric Characters
Variables can only contain alphanumeric characters. Alphanumeric characters consist of
Variable Name Cannot Contain Spaces
Variable names cannot contain spaces. This will break up the name convention.
- $my test
- $test your
As stated before variable may have different types. These type determination is done while assigning value to the variable. In the following example we assign an integer value
9 to the variable named
$age and this will set the type of the
$age = 9
Php have following primitive variable types where we will look all of them in the next chapters.
Variables can be defined in different part of the code or in different files. What will happen if the same variable name defined in different files where these files are important current file. This will create some conflict about scope. Should we access all variables from current file or restrict the scope. We will also look variable scopes in next chapters.