PHP / PHP Variables

In PHP, Variable is a data named used to store a data value and variable starts with the $ sign followed by the name of the variable.

Example
$a = "PHP Variable";

Rules for declaring variables in PHP
1. It starts with the $ sign symbol followed by its name / variable name.
2. Its name must start with a letter / underscore character and can’t be start with a number.
4. Its name can only contain alpha-numeric characters and underscores (_, A-z and 0-9)
5. Its names are case-sensitive ($salary and $SALARY are different variables)

PHP Variables Scope
Any PHP variables can be declared anywhere in the script / program. It refers to the visibility/ availability of variables. I.e For which parts of your program can see / use it. It is very useful to be able to limit a variable's scope to a single function.

PHP variable has 3 scopes. They were
S.No Scope Used / Accessed
1 Local If the variable is declared within a function has a Local Scope and is accessed only within the function.
2 Global If the variable is declared outside a function has a Global Scope and is accessed only outside the function.
3 Static If a variable is declared as static its value will persist enter the program.


Home     Back