In PHP, variables are identifiers, prefixed with a dollar sign ($). A variables is used to hold any types of value, no compile- or runtime type checking on variables. In PHP you can replace a variable's value with another type (no explict conversion require). There is no specific techniq for variable declaration in PHP. For example:
In php you can reference the value of a variable whose name is stored in another variable.For example
When second statement executes, the variable $iop has the value "vip"
In PHP scope of a variable is depend on variable declaration and there accessibility. Basically four types of variable scope in PHP: local, global, static, and function parameters.
A variable declared in side a function is local to that function and can not access outside function.
When run the above PHP code, it will produce following result
In the above example we decleared two variables, inside a function and outside a function with the same name. Then, we are assigning value to the variable decleared outside the function and printing value of the variable. Since variable decleared inside function is local to that function will not increase variable value.
A variable declared outside a function are global and they can be accessed any part of the program. By default variable declared outside function are global and are not available inside function. A global variable must be explicitly declared to be global inside the function.
When run the above PHP code, it will produce following result
Sometimes we need a variable that can maintain the value between function call and available to the program. Then, we need to declare a variable static with the static keyword.
When run the above PHP code, it will produce following result