Home php教程 php手册 PHP Global变量定义当前页面的全局变量实现探讨

PHP Global变量定义当前页面的全局变量实现探讨

Jun 06, 2016 pm 08:30 PM
global variables global variables

我们在这篇文章中就针对PHP Global变量出现的问题给出了一些具体的解决办法,感兴趣的朋友可以参考下哈

PHP Global变量在实际应用中会发现许多问题需要我们不断的去完善处理。我们在这篇文章中就针对PHP Global变量出现的问题给出了一些具体的解决办法。

1:PHP Global变量的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件

复制代码 代码如下:


$a=123;

function aa()
{
Global $a;
//如果不把$a定义为global变量
,函数体内是不能访问$a的
echo $a;
}
aa();


总结:在函数体内定义的PHP Global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用,

复制代码 代码如下:


$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),在别的文件中函数外部 include("config.inc.php"),香港虚拟主机,函数内部需要使用这些变量$a,如果没有声明的话,echo $a是打印不出来任何东西的。因此声明global $a,但是有很多函数和很多变量,总不能不断重复的这样声明吧?有什么好的解决办法,请指点。

answer1:先在config.inc.php里定义常量:define(常量名,常量值),再在其他需要用到的地方require 'config.inc.php',然后就能在这个文件里直接使用这个常量了。
answer2:我也有个办法,美国空间,美国服务器,就是定义数组,如$x[a],$x,那样就只要声明global $x一个了。
answer3:我试了你的这个方法,不行啊。
answer4:改你的php.ini文件。
设置PHP Global变量 为 on
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

New global variable syntax in PHP8.0 New global variable syntax in PHP8.0 May 14, 2023 am 08:33 AM

Recently, PHP8.0 was officially released, and the new version brings many exciting new features. One of the new features that has attracted much attention is the update to the global variable syntax. In older versions of PHP, we introduced a global variable into the current scope by using the global keyword inside a function. In PHP8.0, this method will no longer be recommended, replaced by a new global variable syntax. Traditional way of using global variables: In older versions of PHP, we can use glob

Does Go language have static global variables? Does Go language have static global variables? Jul 11, 2023 pm 03:37 PM

The go language does not have static global variables. It uses a more flexible way to handle the need for global variables. Global variables are usually declared at the package level, that is, variables declared outside the function. These global variables are throughout the package. are visible and can be used in any function in the package.

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

Implementing global variable safety in JavaScript Implementing global variable safety in JavaScript Jun 15, 2023 pm 10:33 PM

As JavaScript becomes more popular, more and more websites and applications rely on JavaScript. However, the use of global variables in JavaScript can have security issues. In this article, I will introduce how to implement global variable safety in JavaScript. The best way to avoid using global variables is to avoid using global variables. In JavaScript, all variables are global by default unless they are declared within a function. Therefore, local variables should be used whenever possible

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

Redeclaration of global variables in C program Redeclaration of global variables in C program Sep 20, 2023 pm 10:29 PM

We will see how C and C++ behave differently when redeclaring a global variable without initialization, redeclaring a global variable with initialization, and redeclaring a global variable and initializing it twice. Additionally, we will repeat the above combination using local variables. 1.A) C program: Re-declare global variables without initialization #include<stdio.h>intvar;intvar;intmain(){ printf("Var=%d",var); return0;} output Var=0B) C++ program:

Can golang functions directly access global variables in goroutine? Can golang functions directly access global variables in goroutine? May 01, 2024 pm 05:51 PM

Yes, Go functions in Goroutine have direct access to global variables by default. Reason: Goroutine inherits the memory space of the Goroutine that created it, including access to global variables.

See all articles