Local and global variables in php
PHP Variables ScopeIn PHP, variables can be declared anywhere in the script. Show
The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes:
Global and Local ScopeA variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: ExampleVariable with global scope: $x = 5; // global scope function myTest() { Variable x inside function is: $x ";} myTest(); echo " Variable x outside function is: $x ";?> Try it Yourself » A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function: ExampleVariable with local scope: function myTest() { Variable x inside function is: $x ";} myTest(); // using x outside the function will generate an error Variable x outside function is: $x ";?> Try it Yourself » You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared. PHP The global KeywordThe To do this, use the Example $x = 5; function myTest() { myTest(); Try it Yourself » PHP also stores all global variables in an array called The example above can be rewritten like this: Example $x = 5; function myTest() { myTest(); Try it Yourself » PHP The static KeywordNormally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the Examplefunction myTest() { myTest(); Try it Yourself » Then, each time the function is called, that variable will still have the information it contained from the last time the function was called. Note: The variable is still local to the function. PHP ExercisesThe scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:
Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example:
This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function. The global keyword First, an example use of Example #1 Using
Sum() The above script will output A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array. The previous example can be rewritten as: Example #2 Using $GLOBALS instead of global
Sum() The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal. Here's an example demonstrating the power of superglobals: Example #3 Example demonstrating superglobals and scope
Using static variablesAnother important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example: Example #4 Example demonstrating need for static variables
This function is quite useless since every time it is called it sets $a to Example #5 Example use of static variables
Now, $a is initialized only in first call of function and every time the Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable $count to know when to stop: Example #6 Static variables with recursive functions
Static variables can be assigned values which are the result of constant expressions, but dynamic expressions, such as function calls, will cause a parse error. Example #7 Declaring static variables
As of PHP 8.1.0, when a method using static variables is inherited (but not overridden), the inherited method will now share static variables with the parent method. This means that static variables in methods now behave the same way as static properties. Example #8 Usage of static Variables in Inherited Methods
References with global and static variables PHP implements the
static and global modifier for variables in terms of references. For example, a true global variable imported inside a function scope with the
test_global_noref() { The above example will output: NULL object(stdClass)#1 (0) { } A similar behaviour applies to the
'Static object: '; The above example will output: Static object: NULL Static object: NULL Static object: NULL Static object: object(stdClass)#3 (1) { ["property"]=> int(1) } This example demonstrates that when assigning a reference to a static variable, it's not remembered
when you call the dodothedreamer at gmail dot com ¶ 10 years ago
warhog at warhog dot net ¶ 16 years ago
$a->func_having_static_var()."\n"; Michael Bailey (jinxidoru at byu dot net) ¶ 18 years ago
B extends A {}$a = new A(); andrew at planetubh dot com ¶ 13 years ago
larax at o2 dot pl ¶ 16 years ago
dexen dot devries at gmail dot com ¶ 5 years ago
Base { pogregoire##live.fr ¶ 5 years ago
teste_global(); gried at NOSPAM dot nsys dot by ¶ 6 years ago
(E_ALL);$GLOB = 0; moraesdno at gmail dot com ¶ 12 years ago
jakub dot lopuszanski at nasza-klasa dot pl ¶ 12 years ago
simon dot barotte at gmail dot com ¶ 5 years ago
$a; jameslee at cs dot nmt dot edu ¶ 17 years ago
jake dot tunaley at berkeleyit dot com ¶ 3 years ago
What is local variable and global variable in PHP?A global variable exists in the program for the entire time the program is executed. A local variable is created when the function is executed, and once the execution is finished, the variable is destroyed. It can be accessed throughout the program by all the functions present in the program.
What is local variable in PHP?A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function −
What is a global variable in PHP?$GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
What is global & local variable?A global variable is a variable that is accessible globally. A local variable is one that is only accessible to the current scope, such as temporary variables used in a single function definition.
|