例如:
复制代码 代码如下:
$a = 1;
include 'b.inc';
?>
这里变量 $a 将会在包含文件 b.inc 中生效。但是,在用户自定义函数中,一个局部函数范围将被引入。任何用于函数内部的变量按缺省情况将被限制在局部函数范围内,此时为局部变量。
PHP 中全局变量在函数中使用时必须申明为global。
在函数中使用global声明的变量即为全局变量,可以在函数外使用。注意:global声明变量时,不能直接对变量赋值,需要先声明后赋值。
在全局范围内,也可以通过$GLOBALS访问全局变量,在函数内不需要使用global关键字来访问全局变量了。$GLOBALS 是一个关联数组,每一个变量为一个元素,键名对应变量名,值对应变量的内容。$GLOBALS 之所以在全局范围内存在,是因为 $GLOBALS 是一个超全局变量。
常量可以不用理会变量的作用域而在任何地方定义和访问;
变量范围的另一个重要特性是静态变量(static variable)。静态变量仅在局部函数域中存在,但当程序执行离开此作用域时,其值并不丢失。静态变量仅在第一次调用时进行初始化,声明时可以对其赋值,不能是表达式值。如果在声明中用表达式的结果对其赋值会导致解析错误。
当把一个引用(带上&的变量或对象)赋值给一个静态变量时,引用并不是静态地存储的,第二次调用函数时静态变量其值并没有被记住。同样,当把一个引用(带上&的变量或对象)赋值给global变量时,此变量的变化对函数外不起作用,作用范围只在该函数内。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

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

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
