This article will introduce in detail the method of global variable global in PHP. Friends who need to know how to use the global function can refer to this article.
The scope of a variable is the context in which it is defined (that is, its effective scope). Most PHP variables have only a single scope. This single scope span also includes files introduced by include and require. For example:
The code is as follows | Copy code | ||||
include 'b.inc'; ?>
|
代码如下 | 复制代码 |
$a = 1; /* global scope */ function Test() Test(); |
The code is as follows | Copy code |
$a = 1; /* global scope */ function Test() Test(); |
代码如下 | 复制代码 |
$a = 0 ; function Test() { $a =1; } Test(); echo $a; ?> |
Today I encountered the problem that php global variables do not work.
代码如下 | 复制代码 |
$a = 0 ; function Test() { global $a;//申明函数体Test内使用的$a变量为global全局变量 $a =1; } Test(); echo $a; ?> |
After declaring the $a variable used in the function body Test as a global global variable, $a has a global effect, so the output is 1.
The above examples are just basic knowledge of global variables. Let’s look at more complicated ones:
//A.php file
The code is as follows
|
Copy code
|
||||||||
function Test_Global() {Include 'B.php'; Test();}
Test_Global(); echo $a;
Related labels:
source:php.cn
Previous article:PHP interview questions: Access control_PHP tutorial
Next article:php set_time_limit() sets page execution time_PHP tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|