PHP STATIC VARIABLES
What is a variable?
Variables in a program are used to store a few qualities or information that can be used later in a program. The variables are additionally similar to compartments that store character values, numeric qualities, memory locations, as well as strings. PHP has its own specific manner of announcing and putting away variables.
There are not many standards that should be followed and realities that should be remembered while managing variables in PHP:
Data types utilized by PHP to declare or build variables:
The extent of a variable is characterized as its degree in the program inside, which it very well may be gotten to. For example, the extent of a variable is the part of the program inside which it is apparent or can be gotten to.
Contingent upon the degrees, PHP has three variable extensions:
The variables pronounced inside a function are called nearby variables to that function and have its extension just in that specific function. In short words, it can't be gotten outside that function. Any statement of a variable external to the function with the same name as that of the one inside the function is something else entirely. We will find out about capacities exhaustively in later articles. For the present, think about a function as a square of explanations.
Example of Local Variables
In the above example, we have created a local variable $num and displayed the use of the local variable both inside the function as well as outside it and found that it can only work in the local scope of a function.
The variables announced externally in a function are called global variables. These variables can be gotten to straightforwardly outside a function. To get access inside a function, we want to utilize the "global" catchphrase before the variable to allude to the global variable.
Example of Global Variables
In the above example, we have created a global variable $num and display the use of the global variable both inside the function as well as outside it.
The last kind of factor perusing that I examine is known as static. Rather than the variables announced as function boundaries, which are annihilated on the function's leave, a static variable won't lose its worth when the function exits and will, in any case, hold that worth should the function be called once more.
You can pronounce a variable to be static just by putting the watchword STATIC before the variable name.
A static variable is the attribute of PHP to erase the variable once it finishes its execution and the memory is liberated. However, in some cases, we really want to store the variables even after the fulfillment of function execution. To do this, we utilize a static catchphrase, and the variables are then called static variables. PHP partners an information type contingent upon the incentive for the variable.
This will deliver the accompanying outcome ?
You must have noticed that $ regularly counts increments even after the first function call, but This is because $sum is static, and its memory is not freed after the execution of the first function call.
Example of Static Variables
You probably saw that $num routinely augments even after the main function call, yet $sum doesn't. This is on the grounds that $sum isn't static, and its memory is liberated after the execution of the first function call.