Variables are placeholders for information to facilitate the programmer with named storage to be used in the program. They play an important role in a program because they enable us to store, reference and manipulate information in a readable way throughout the lifetime of a program.
In PHP variable names always begin with a $ sign followed by an alphanumeric name.
Example :
In the above example the variable $msg holds the value "Learn PHP because it is the best", the variable $number 1 holds the value 30 and $number2 holds the value 50.
It can be observed in the above example that the variables were never declared. This is because PHP does not require explicit declaration of variables and their types. Simply using a variable implies that a variable has been created and its datatype is assumed from the initial value that it is assigned.
Naming Convention for PHP variables
PHP requires us to follow some rules while naming a variable:
- Variable names always begin with a $ sign followed by the name itself.
- The name could either be a short name or it could be a longer descriptive name.
- Variables usually have descriptive names making the program more intuitive and meaningful.
- Variable names need not be declared and no explicit data type of the variable needs to be mentioned. Hence PHP assumes the datatype of a variable based on its initial assignment.
- Variable names can consist of alphanumeric characters and underscore (A-z, 0-9, and _ ).
- Variable names always start with an alphabet or an underscore and cannot begin with a number.
- PHP variable names are always case-sensitive. Eg $name is different from $NAME and they are both 2 separate variables.
PHP – A Loosely Typed Language
Languages like C, C++, Java require that the programmer explicitly declare variables and their datatypes. Although we can declare a variable in PHP it is not mandatory. We can start using a variable directly without declaring it, because of which it is called a loosely typed language.
Whenever we use a variable in PHP without declaring it PHP converts it to the correct datatype by looking at the value that the variable is initially assigned.
Example:
Here the variable $msg is assumed to be of type String because it is assigned a string value.
Output a Variable
There are several ways to output the value of a variable during the execution of a program. The ‘Echo’ command is one of the most common ways to output the value of a variable during program execution and because of this we will be using it for all our examples. Using the ‘Echo’ command we can output variables of any datatype.
Example :
The above example outputs a string
The above example outputs a number.
Variable Scope in PHP
The scope of a variable refers to the portions of the program where we can eference the variable. Hence the scope of a variable is also known as the visibility of the variable.
PHP allows variables to be declared anywhere in the program. Depending on their visibility variables can have 3 different scopes of local, global and static.
Global Scope
A variable having a global scope is visible or accessible throughout the program outside functions. Hence it is called a global variable.
It gets hidden inside a function because of which we can create variables inside a function which have the same name as the global variable.
Example :
The output will be
Variable test_var inside function is:
Variable test_var outside function is:15
Because the value of test_var has been set outside of the function and so it is not available inside the function unless we explicitly define it inside the function using the global keyword as below.
Global Keyword
The global keyword is used to access global variables inside a function.
Example :
The output will be
Variable test_var inside function is:15
Variable test_var outside function is:15
Global Variable
All the global variables are stored in an array called the $GLOBALS[index]. Here index is the name of the variable. We can access and update global variables inside functions using this array.
Example :
Local Scope
Any variable which is declared within a function is called a local variable. Local variables have local scope. They can be accessed and updated only within the function that they are declared in.
Example :
The output will be "Variable local_variable outside function is:" as $local_variable does not exist outside.
The Static Keyword and Static Variables
Variables which are local to a function lose their value once the function execution is over. However, sometimes we need to retain the value of the variables throughout the life of the program even after the function execution.
Hence, we can declare the function value to be static using the STATIC keyword. A static variable is only accessible inside the function i.e. it still has local scope only even though its value is retained after the function completes execution.
Example:
Code: