国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP Global與$GLOBALS變量作用域與區(qū)別

PHP Global與$GLOBALS變量作用域與區(qū)別

May 25, 2016 pm 04:48 PM
global variable

在php中變量有很多種如普通的變量及全局變量(Global與$GLOBALS),本文章來給大家介紹在php中Global與$GLOBALS的用法區(qū)別.

Global,全局變量

PHP Global變量在實際應(yīng)用中會發(fā)現(xiàn)許多問題需要我們不斷的去完善處理.我們在這篇文章中就針對PHP Global變量出現(xiàn)的問題給出了一些具體的解決辦法,PHP hack的使用技巧詳解,代碼實現(xiàn)PHP GTK寫文本查看器,網(wǎng)站開發(fā)中PHP語言優(yōu)缺點.

如何正確實現(xiàn)PHP function函數(shù)擴展

PHP error_log()函數(shù)處理錯誤日志

1:PHP Global變量的作用是定義全局變量,但是這個全局變量不是應(yīng)用于整個網(wǎng)站,而是應(yīng)用于當前頁面,包括include或require的所有文件

實例代碼如下:

$a=123;
function aa()  
{  
    Global $a;   
    //如果不把$a定義為global變量,函數(shù)體內(nèi)是不能訪問$a的  
    echo $a;  
}  
aa();

總結(jié):

在函數(shù)體內(nèi)定義的PHP Global變量,函數(shù)體外可以使用,在函數(shù)體外定義的global變量不能在函數(shù)體內(nèi)使用,

實例代碼如下:

$glpbal $a;   
$a=123;    
function f()   
{   
    echo $a; //錯誤,   
}

再看看下面一例,實例代碼如下:

function f()   
{   
    global $a;  $a=123;   
}    
f();   
echo $a;
//正確,可以使用

2:PHP Global變量問題解析:

question:我在config.inc.php中定義了一些變量($a),在別的文件中函數(shù)外部 include("config.inc.php"),函數(shù)內(nèi)部需要使用這些變量$a,如果沒有聲明的話,echo $a是打印不出來任何東西的.因此聲明global $a,但是有很多函數(shù)和很多變量.總不能不斷重復的這樣聲明吧?有什么好的解決辦法,請指點.

answer1:先在config.inc.php里定義常量:define(常量名,常量值),再在其他需要用到的地方require 'config.inc.php',然后就能在這個文件里直接使用這個常量了.

answer2:我也有個辦法,就是定義數(shù)組,如$x[a],$x,那樣就只要聲明global $x一個了.

answer3:我試了你的這個方法,不行啊.

answer4:改你的php.ini文件.

設(shè)置PHP Global變量 為 on,下面我們看看復雜點的:

實例代碼如下:

//A.php 文件
<?php
function Test_Global() {
    include &#39;B.php&#39;;
    Test();
}
$a = 0;
Test_Global();
echo $a;
?>
//B.php 文件 
<?php
function Test() {
    global $a; //申明函數(shù)體Sum內(nèi)使用的$a變量為global全局變量
    $a = 1;
}
?>

為什么輸出的卻是0?!!

在用戶自定義函數(shù)中,一個局部函數(shù)范圍將被引入.任何用于函數(shù)內(nèi)部的變量按缺省情況將被限制在局部函數(shù)范圍內(nèi)(包括include 和 require 導入的文件內(nèi)的變量)!

解釋:A.php文件的內(nèi)Test_Global是定義好的第三方函數(shù),該函數(shù)用include導入了B.php文件內(nèi)的$a的global全局變量,所以$a被限制在Test_Global局部函數(shù)范圍內(nèi),所以B.php文件內(nèi)的$a的作用范圍都在Test_Global內(nèi),而不是作用了整個A.php內(nèi)….

解決方案:

1. 沖出局部函數(shù)實例代碼如下:

//A.php 文件 
<?php
function Test_Global() {
    Test();
}
include &#39;B.php&#39;; //將include 從局部Test_Global函數(shù)中移出
$a = 0;
Test_Global();
echo $a;
?>
//B.php 文件 
<?php
function Test() {
    global $a;
    $a = 1;
}
?>

global和$GLOBALS的區(qū)別

php中g(shù)lobal和$GLOBALS不僅僅是寫法不一樣以為,2者的區(qū)別還是很大的,在實際應(yīng)用中需要注意!

先看下面的實例代碼:

<?php
// 例子1
function test_global() {
    global $var1, $var2;
    $var2 = & $var1;
}
function test_globals() {
    $GLOBALS[&#39;var3&#39;] = & $GLOBALS[&#39;var1&#39;];
}
$var1 = 5;
$var2 = $var3 = 0;
test_global();
print $var2 . "\n";
test_globals();
print $var3 . "\n";
?>

執(zhí)行結(jié)果為:0 5

怎么會這樣呢?不應(yīng)該是2個5嗎?怎么會出現(xiàn)1個0和1個5呢?

恩,我們保留以上問題,深入分析$GLOBALS和global的原理!我們都知道變量其實是相應(yīng)物理內(nèi)存在代碼中的"代號"而已引用php手冊的$GLOBALS的解釋:

Global 變量:$GLOBALS,注意: $GLOBALS 在 PHP 3.0.0 及以后版本中適用.由所有已定義全局變量組成的數(shù)組.變量名就是該數(shù)組的索引.這是一個"superglobal",或者可以描述為自動全局變量.也就是說上面代碼中的$var1和$GLOBALS['var1']是指的同一變量,而不是2個不同的變量!下面來分析global到底做了什么?

引用php手冊的global的解釋:

如果在一個函數(shù)內(nèi)部給一個聲明為 global 的變量賦于一個引用,該引用只在函數(shù)內(nèi)部可見.可以通過使用 $GLOBALS 數(shù)組避免這一點.我們都知道php中的函數(shù)所產(chǎn)生的變量都是函數(shù)的私有變量,那么global關(guān)鍵字產(chǎn)生的變量也肯定逃不出這個規(guī)則,為什么這么說呢,

請看下面的代碼:

<?php
// 例子2
function test() {
    global $a;
    unset($a);
}
$a = 1;
test();
print $a;
?>

執(zhí)行結(jié)果為:1

為什么會輸出1呢?不是已經(jīng)把$a給unset了嗎?unset失靈了?php的bug?

都不是,其實unset起作用了,是把test函數(shù)中的$a給unset掉了,可以在函數(shù)test()中加入print $a;來測試!

接著回到上面的例子1,看test_global中的這一代碼"$var2 = &$var1;",上面是一個引用賦值運算,也就是$var2將指向var1所指向的物理內(nèi)存地址,所以例子1執(zhí)行過test_global函數(shù)以后,變量的變化只在函數(shù)的局部產(chǎn)生效應(yīng),在函數(shù)外部$var2的指向物理內(nèi)存地址并沒有變化,還是它自己.此時,就能理解為什么例子1執(zhí)行完以后,$var2是0,而$var3是5了!

所以我們得出一個結(jié)論,在函數(shù)中g(shù)lobal和$GLOBALS[]的區(qū)別在于:

global在函數(shù)產(chǎn)生一個指向函數(shù)外部變量的別名變量,而不是真正的函數(shù)外部變量,一但改變了別名變量的指向地址,就會發(fā)生一些意料不到情況,例如例子1.

$GLOBALS[]確確實實調(diào)用是外部的變量,函數(shù)內(nèi)外會始終保持一致,可以對照下面兩個列子再加深下印象:

global:

實例代碼如下:

<?php
function myfunction() {
    global $bar;
    unset($bar);
}
$bar = "someting";
myfunction();
echo $bar;
?>

輸出:someting

$GLOBALS[]:

<?php
function foo() {
    unset($GLOBALS[&#39;bar&#39;]);
}
$bar = "something";
foo();
echo $bar;
?>

輸出:空

當按照上面的思路理解后,碰到下面的情況是不是又有些暈?zāi)?

實例代碼如下:

<?php
$a = 1;
$b = 2;
function Sum() {
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b;
?>

輸出將是 "3".在函數(shù)中申明了全局變量 $a 和 $b,任何變量的所有引用變量都會指向到全局變量.

怎么不是2呢,在函數(shù)外部不是不影響嗎,請注意$b在函數(shù)中并沒有通過引用修改,而是修改的$b指向物理內(nèi)存的值,因此外部輸入為3.

php中g(shù)lobal和$GLOBALS不僅僅是寫法不一樣以為,2者的區(qū)別還是很大的,在實際應(yīng)用中需要注意!

先看下面的PHP代碼例子:

<?php
// 例子1
function test_global() {
    global $var1, $var2;
    $var2 = & $var1;
}
function test_globals() {
    $GLOBALS[&#39;var3&#39;] = & $GLOBALS[&#39;var1&#39;];
}
$var1 = 5;
$var2 = $var3 = 0;
test_global();
print $var2 . "\n";
test_globals();
print $var3 . "\n";
?>

執(zhí)行結(jié)果為:0 5

怎么會這樣呢?不應(yīng)該是2個5嗎?怎么會出現(xiàn)1個0和1個5呢?

恩,我們保留以上問題,深入分析$GLOBALS和global的原理!我們都知道變量其實是相應(yīng)物理內(nèi)存在代碼中的"代號"而已,引用php手冊的$GLOBALS的解釋:

Global 變量:$GLOBALS,注意: $GLOBALS 在 PHP 3.0.0 及以后版本中適用.

由所有已定義全局變量組成的數(shù)組.變量名就是該數(shù)組的索引.這是一個"superglobal",或者可以描述為自動全局變量.也就是說上面代碼中的$var1和$GLOBALS['var1']是指的同一變量,而不是2個不同的變量!

下面來分析global到底做了什么?

引用php手冊的global的解釋:

如果在一個函數(shù)內(nèi)部給一個聲明為 global 的變量賦于一個引用,該引用只在函數(shù)內(nèi)部可見.可以通過使用 $GLOBALS 數(shù)組避免這一點.我們都知道php中的函數(shù)所產(chǎn)生的變量都是函數(shù)的私有變量,那么global關(guān)鍵字產(chǎn)生的變量也肯定逃不出這個規(guī)則,為什么這么說呢,看下面的代碼:

實例代碼如下:

<?php
// 例子2
function test() {
    global $a;
    unset($a);
}
$a = 1;
test();
print $a;
?>

執(zhí)行結(jié)果為:1

為什么會輸出1呢?不是已經(jīng)把$a給unset了嗎?unset失靈了?php的bug?

都不是,其實unset起作用了,是把test函數(shù)中的$a給unset掉了,可以在函數(shù)test()中加入print $a;來測試!

接著回到上面的例子1,看test_global中的這一代碼"$var2 =& $var1;",上面是一個引用賦值運算,也就是$var2將指向var1所指向的物理內(nèi)存地址,所以例子1執(zhí)行過test_global函數(shù)以后,變量的變化只在函數(shù)的局部產(chǎn)生效應(yīng),在函數(shù)外部$var2的指向物理內(nèi)存地址并沒有變化,還是它自己.

此時,就能理解為什么例子1執(zhí)行完以后,$var2是0,而$var3是5了!

所以我們得出一個結(jié)論,在函數(shù)中g(shù)lobal和$GLOBALS[]的區(qū)別在于:

global在函數(shù)產(chǎn)生一個指向函數(shù)外部變量的別名變量,而不是真正的函數(shù)外部變量,一但改變了別名變量的指向地址,就會發(fā)生一些意料不到情況,例如例子1.

$GLOBALS[]確確實實調(diào)用是外部的變量,函數(shù)內(nèi)外會始終保持一致,可以對照下面兩個列子再加深下印象:

global:

實例代碼如下:

<?php
function myfunction() {
    global $bar;
    unset($bar);
}
$bar = "someting";
myfunction();
echo $bar;
?>

輸出:someting

實例代碼如下:

$GLOBALS[]:

<?php
function foo() {
    unset($GLOBALS[&#39;bar&#39;]);
}
$bar = "something";
foo();
echo $bar;
?>

輸出:空

當按照上面的思路理解后,碰到下面的情況是不是又有些暈?zāi)?

實例代碼如下:

<?php
$a = 1;
$b = 2;
function Sum() {
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b;
?>

輸出將是 "3".在函數(shù)中申明了全局變量 $a 和 $b,任何變量的所有引用變量都會指向到全局變量.

怎么不是2呢,在函數(shù)外部不是不影響嗎,請注意$b在函數(shù)中并沒有通過引用修改,而是修改的$b指向物理內(nèi)存的值,因此外部輸入為3


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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

11 Ways to Set Environment Variables on Windows 3 11 Ways to Set Environment Variables on Windows 3 Sep 15, 2023 pm 12:21 PM

Setting environment variables on Windows 11 can help you customize your system, run scripts, and configure applications. In this guide, we'll discuss three methods along with step-by-step instructions so you can configure your system to your liking. There are three types of environment variables System environment variables – Global variables are the lowest priority and are accessible to all users and applications on Windows and are typically used to define system-wide settings. User Environment Variables – Higher priority, these variables only apply to the current user and process running under that account, and are set by the user or application running under that account. Process environment variables – have the highest priority, they are temporary and apply to the current process and its sub-processes, providing the program

See all articles