PHP全局变量与超级全局变量区别分析,php全局变量区别
PHP全局变量与超级全局变量区别分析,php全局变量区别
本文分析了PHP全局变量与超级全局变量区别。分享给大家供大家参考,具体如下:
全局变量就是在函数外面定义的变量。不能在函数中直接使用。因为它的作用域不会到函数内部。所以在函数内部使用的时候常常看到类似global $a;
超全局变量作用域在所有脚本都有效。所以,在函数可直接使用。比如 $_GET,$_SERVER都是超全局变量。除$_GET,$_POST,$_SERVER,$_COOKIE等之外的超全局变量保存在$GLOBALS数组中。注意,不要误写成$_GLOBALS。有点受到$_POST的影响了。
$GLOBAL是一个特殊的php自定义的数组。超级全局变量。跟$_SERVER一样,都属于超全局变量。
在手册中搜索$GLOBALS 就能查询超全局变量的具体说明和使用方法
变量名就是该数组的索引。 说明:使用$GLOBALS['a']定义一个超全局变量后。可以使用$a直接访问
手册中这样解释的:
$GLOBALS — References all variables available in global scope 引用所有在全局范围内的可用的变量
结合例子这样理解:假如在函数外面定义了一个变量$a。那么在函数中可用通过$GLOBALS['a']获取到这个变量的值。所以,$GLOBALS数组里面就是:用户定义的所有全局变量。
为什么称它为超级全局变量。对比$_POST,在函数里面可用直接使用。根本不需要使用global语句了。所以$GLOBALS也是这样的原理,只是$_POST保存是post方式传递的变量。$GLOBALS保存的是用户定义的全局变量。
个人理解:
之前对超级全局变量理解不对:超级全局变量在所有脚本下有效,这样表述导致了理解上细微的偏差。如果在所有脚本下有效,那么是不是$GLOBALS保存的超级全局变量,在一个php文件中定义后,在另一个文件中还能获取到。
显然不会是这样的。估计受到了$_POST的影响,因为它好像所有人都能去访问到。没有意识到,你只能访问到当前处理到的$_POST数据。不可能两个人同时提交留言,php文件获取到post的数据怎么区别。这个数据只能是当前线程的。最后,超级全局变量可以这样看,因为它是相对于全局变量而言的。比全局变量更一个层次,全局变量不能作用到函数内部。它就解决了这个问题。在其他语言中,全局变量都能作用到函数内部。php语言设计就不是这样的。
全局变量直接在函数内使用,那么总得提供一种变量能直接使用的吧。就出来了超级全局变量的概念。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- PHP Global定义全局变量使用说明
- 解析在PHP中使用全局变量的几种方法
- php数组声明、遍历、数组全局变量使用小结
- PHP超级全局变量数组小结
- PHP中全局变量global和$GLOBALS[]的区别分析
- 认识并使用PHP超级全局变量
- php 全局变量范围分析
- PHP中函数内引用全局变量的方法
- 用php实现像JSP,ASP里Application那样的全局变量
- PHP中的超全局变量

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
