NOTE: This is a reference question dealing with variable scope in PHP. Please close any of the many questions that fit this pattern as duplicates of this question.
What is "variable scope" in PHP? Can variables in one .php file be accessed in another .php file? Why do I sometimes get an "undefined variable" error?
Although variables defined within a function scope cannot be accessed from the outside, this does not mean that you cannot use their values after the function completes. PHP has a well-known
static
keyword which is widely used in object-oriented PHP to define static methods and properties, but it should be remembered thatstatic
can also be used inside a function to define static variables .What is a "static variable"?
Static variables are different from ordinary variables defined in function scope. Static variables will not lose their value when program execution leaves the scope. Let us consider the following example using static variables:
result:
If we define
$counter
withoutstatic
, then the value echoed each time will be the same as the$num
parameter passed to the function. This simple counter can be built usingstatic
without additional workarounds.Static variable use case
Skill
Static variables only exist in local function scope. it can't be Accessed outside the function in which it is defined. so you can Make sure it will keep its value until the next call This function.
Static variables can only be defined as scalars or scalars Expressions (since PHP 5.6). inevitably assign other values to it Which results in failure, at least as of this writing. However, you can do this on the next line of code:
result:
Static functions are somewhat "shared" between the object's methods Classmates. It’s easy to understand by looking at the following example:
This only applies to objects of the same class. Static variables will behave as expected if the objects are from different classes (or even extend each other).
Are static variables the only way to retain values between function calls?
Another way to preserve values between function calls is to use closures. Closures were introduced in PHP 5.3. In short, they allow you to restrict access to a certain set of variables within a function's scope to another anonymous function, which will be the only way to access them. Being inside a closure variable may emulate (more or less successfully) OOP concepts such as "class constants" (if they are passed by value in the closure) or "private properties" (if passed by reference) in structured programming .
The latter actually allows the use of closures instead of static variables. It's always up to the developer to decide what to use, but it should be mentioned that static variables are definitely useful when using recursion and deserve developer attention.
What is "variable scope"?
The "scope" or "where they can be accessed" of variables is limited. Just because you wrote $foo = 'bar'; once somewhere
How to define scope in PHP?somewhere in your application, doesn't mean you can reference
$ from foo>Elsewhere
within the application. The variable $foohas a certain scope, within which it is valid, and only code within the same scope can access the variable.
function scope. This is the only range delimiter present in PHP. Variables inside a function are only available inside that function. Variables outside a function can be used anywhere outside the function, but cannot be used inside any function. This means that there is a special scope in PHP: the global scope. Any variable declared outside any function is in this global scope.
Example:$foo
File boundariesis in the
global scope and $bazis in the
localscope of myFunc. Only code within myFunc can access
$baz
. Only code outsidemyFunc
can access
$foo. Neither party can access the other:Scope and included files
Not separated
Range:a.php
functions
only. For scope purposes, you might consider including files like copy and paste code:
c.php
myFunc
Each new
function, and any variables in
a.phponly have local function scope. Just because they
appear to be in global scope withina.php
doesn't necessarily mean that they are, it really depends on the context in which the code is contained/executed. What about functions and functions in classes?declaration introduces a new scope, simple as that.
Function within (anonymous) function
course
What is the use of scope?
Dealing with scoping issues may seem annoying, but
limited variable scope is critical to writing complex applications!If every variable you declare is available from anywhere else within your application, then you'll be stepping through everything on your variables with no real way to keep track of what changed what. You are limited in the sensible names you can give your variables, and you may want to use the variable "
$name" in more than one place. If you can only use this unique variable name once in your application, then you have to employ a very complex naming scheme to ensure that your variables are unique and that you don't change the wrong variable from the wrong piece of code. observe:
What does the above function do if there is no scope? $bar
Where does it come from? What status does it have? Is it even initialized? Do I need to check it every time? This is not maintainable. Which brings us to...
Crossing range boundaries
The correct way: passing in and out variables
Variables
$bar
explicitly enters this range as function parameters. Just looking at this function, it's clear where the values it uses come from. It then explicitly returns a value. The caller has confidence that it knows which variables the function will use and where its return value comes from:Extend the scope of variables to anonymous functions
Anonymous functions explicitly include
$foo
in their surrounding scope. Note that this is not the same as global scope.Wrong way:
Global
As mentioned earlier, the global scope is somewhat special, and functions can explicitly import variables from it:
This function uses and modifies global variables
$foo
. Do not do this! (Unless you really, really know what you're doing, and even then: don't do it!)What the caller of this function sees is this:
There is no indication that this function has any side effects, but it does. This can easily become confusing because some functions are constantly modifying and require some global state. You want your functions to be stateless, acting only on their inputs and returning defined outputs, no matter how many times you call them.
You should avoid using the global scope in any way if possible; most certainly, you should not "pull" variables from the global scope to the local scope.