Home > Backend Development > PHP Tutorial > What is the variable scope of php

What is the variable scope of php

怪我咯
Release: 2023-03-10 20:18:01
Original
1186 people have browsed it

The scope of a variable is the context in which it is defined (that is, the scope in which it takes effect). Most PHP variables have only a single scope. This single range span also includes files introduced by include and require

For example:

The code is as follows:

<?php 
$a = 1; 
include &#39;b.inc&#39;; 
?>
Copy after login

Here the variable $a will be in Include files Valid in b.inc. However, in user custom functions, a local function scope will be introduced. Any variables used inside a function will be limited to the scope of the local function by default, and are local variables at this time.
 
  Global variables in PHP must be declared as global when used in functions.
Variables declared using global in a function are global variables and can be used outside the function. Note: When declaring a variable globally, you cannot directly assign a value to the variable. You need to declare it first and then assign a value.

In the global scope, global variables can also be accessed through $GLOBALS. There is no need to use the global keyword to access global variables within a function. $GLOBALS is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the variable's content. The reason $GLOBALS exists in the global scope is because $GLOBALS is a superglobal variable.
Constants can be defined and accessed anywhere regardless of the variable scope;

Another important feature of variable scope is static variables (static variable ). Static variables only exist in the local function scope, but their values ​​are not lost when program execution leaves this scope. Static variables are only initialized on the first call. They can be assigned values ​​when declared, but cannot be expression values. Assigning it with the result of an expression in a declaration will result in a parsing error.

When a reference (a variable or object with &) is assigned to a static variable, the reference is not stored statically, and the value of the static variable is not remembered the second time the function is called. Similarly, when a reference (variable or object with &) is assigned to a global variable, the change of this variable has no effect outside the function, and the scope is only within the function.

The above is the detailed content of What is the variable scope of php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template