PHP中全局变量global详解
本文章来详细的介绍关于PHP中全局变量global的方法,有需要了解global函数使用方法的朋友可参考本文章.
变量的范围即它定义的上下文背景(也就是它的生效范围).大部分的 PHP 变量只有一个单独的范围.这个单独的范围跨度同样包含了 include 和 require 引入的文件.
实例代码如下:
<?php $a = 1; include 'b.inc'; ?>
这里变量 $a 将会在包含文件 b.inc 中生效.但是,在用户自定义函数中,一个局部函数范围将被引入.任何用于函数内部的变量按缺省情况将被限制在局部函数范围内.例如:
实例代码如下:
<?php $a = 1; /* global scope */ function Test() { echo $a; /* reference to local scope variable */ } Test(); ?>
这个脚本不会有任何输出,因为 echo 语句引用了一个局部版本的变量 $a,而且在这个范围内,它并没有被赋值.你可能注意到 PHP 的全局变量和 C 语言有一点点不同,在 C 语言中,全局变量在函数中自动生效,除非被局部变量覆盖.这可能引起一些问题,有些人可能不小心就改变了一个全局变量.PHP 中全局变量在函数中使用时必须申明为global.
今天就遇到了php 全局变量不起作用的问题.先上一段简单的代码:
实例代码如下:
<?php $a = 0; function Test() { $a = 1; } Test(); echo $a; ?>
上面的代码中输出是0,那是因为函数体Test内$a变量被缺省设置为局部变量,$a的作用域就是在Test内.修改代码如下
实例代码如下:
<?php $a = 0; function Test() { global $a; //申明函数体Test内使用的$a变量为global全局变量 $a = 1; } Test(); echo $a; ?>
申明函数体Test内使用的$a变量为global全局变量后,使得$a的作用全局,所以输出为1.
上面的实例只是基本的global全局变量知识,下面我们看看复杂点的:
//A.php 文件
实例代码如下:
<?php function Test_Global() { include 'B.php'; Test(); } $a = 0; Test_Global(); echo $a; ?>
//B.php 文件
<?php function Test() { global $a; //申明函数体Sum内使用的$a变量为global全局变量 $a = 1; } ?>
为什么输出的却是0?!!
在用户自定义函数中,一个局部函数范围将被引入.任何用于函数内部的变量按缺省情况将被限制在局部函数范围内(包括include 和 require 导入的文件内的变量)!
解释:A.php文件的内Test_Global是定义好的第三方函数,该函数用include导入了B.php文件内的$a的global全局变量,所以$a被限制在Test_Global局部函数范围内,所以B.php文件内的$a的作用范围都在Test_Global内,而不是作用了整个A.php内….
解决方案:
1. 冲出局部函数
实例代码如下:
//A.php 文件
<?php function Test_Global() { Test(); } include 'B.php'; //将include 从局部Test_Global函数中移出 $a = 0; Test_Global(); echo $a; ?>
//B.php 文件
<?php function Test() { global $a; $a = 1; } ?>
2.优秀的访问器
实例代码如下:
//A.php 文件
<?php include 'B.php'; $a = 0; Set_Global($a); echo $a; ?>
//B.php 文件
<?php function Set_Global(&$var) { $var = 1; } ?>
文章网址:
随意转载^^但请附上教程地址。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

When we write web pages using PHP, sometimes we need to include code from other PHP files in the current PHP file. At this time, you can use the include or include_once function to implement file inclusion. So, what is the difference between include and include_once?

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages or frameworks, require is used to perform specific tasks or functions.

Xiaomi has introduced another WLAN mesh system for the global market. After the AC1200 router (approx. 69 euros on Amazon) went on sale in this country, the Xiaomi Mesh System AC1200 has now been announced. The new product has recently been listed on

Steps to resolve fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear') in PHP header When developing websites or applications using PHP, we often encounter various errors . One of the common errors is "fatalerror:require():Failed

HashKey, the first licensed cryptocurrency exchange in Hong Kong, announced on X platform on February 18 that its international trading platform HashKeyGlobal has officially launched BNB (Binance Coin). "HashKeyGlobal added spot trading: BNB welcomes new users: deposit or trade BNB to participate in the HK$10,000 prize pool and win a HK$10 reward (first come first served). BNB deposit and withdrawal: Opened BNB/USDT spot trading: 8:00 UTC time on February 18th" Binance founder Zhao Changpeng (CZ) expressed recognition of BNB's listing on HashKeyGlobal Binance founder Zhao Changpeng (CZ) commented that BNB was in HashKeyGlobal

Xiaomi recently listed the Xiaomi Soundbar 2.0ch on its global website, which indicates that a global launch is pending, although there is no concrete information on this yet. As usual, Xiaomi reveals all technical specs, but has not provided any det

Steps to resolve FatalError:require():Failedopeningrequired'data/tdk.php' in PHP header When developing and maintaining PHP websites, we often encounter various errors and exceptions. One of the common errors is "FatalError:require():Failedopeningrequired'data/tdk.php'".
