[rps-include post=6522]
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.
$ Sign
All variables start with $
sign. It is like variable selector. Following example show valid and invalid variable named.
Valid
- $age
- $test1
- $NAME
- $SiZe
Invalid
- age
- a$age
- $test$
- $9name
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.
Valid
- $_age
- $Test
- $My_Test
Invalid
- $.test
- $-age
Variable Name Cannot Start with Number
Variables can not start with numbers. This is one of the pitfalls new developers or learner fall.
Valid
- $age1
- $T1st
Invalid
- $1
- $1Test
- $2age2
Variable Name Contain Alphanumeric Characters
Variables can only contain alphanumeric characters. Alphanumeric characters consist of A-z
, 0-9
, _
.
Variable Name Cannot Contain Spaces
Variable names cannot contain spaces. This will break up the name convention.
Valid
- $my_test
- $yourtest
Invalid
- $my test
- $test your
Variable Types
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
as integer
automatically.
$age = 9
Php have following primitive variable types where we will look all of them in the next chapters.
- String
- Integer
- Float
- Double
- Boolean
- Null
Variable Scope
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.
[rps-include post=6522]