PHP Variables-Declare Initialize Local, Global, Static Variables

Variables are those which is used to store the information. They act like a container.

It start with ‘$’ this sign and follow by name of the variable.

Variable name should either start with Underscore or some letter but I can never start with a number.

Example:

Result:

5  Price is 66.5 My Name is Ram

Explanation: $a stored a numeric value $price hold fractional value and $name hold a string value.

Now moving further, PHP has three different types of variable scopes in a program. The three are :

Global Variable in PHP

The variable which is declared outside the function then is called Global variable.

This variable can be access outside the particular function.

Example:

Result:
[12.55]

Local Variables in PHP

The variable which is declared inside the function then is called Local variable. This variable can be only access inside the function.

Example:

Output

Explanation: The variable was declared inside the function will not be call up outside the function, it can only be called at inside the function. While calling the local variable outside the function will give some error.

Static variable in PHP

Example:

Output

Explanation: Here i is static variable and j is local variable.

Static variable initialized once and on each function call it is getting incremented.