Home Backend Development PHP Tutorial 关于全局变量global懂得和疑惑

关于全局变量global懂得和疑惑

Jun 13, 2016 pm 12:59 PM
echo function global nbsp test

关于全局变量global理解和疑惑?
php刚刚起步,对global产生了疑问,然后自己试验了下:

function test()<br />
{<br />
	global $a;<br />
	$a=10;<br />
	global $b;<br />
    $b=$a+10;   <br />
}<br />
test();<br />
echo $a."<br>";<br />
echo $b."<br>";<br />
?>
Copy after login

恩,效果很好,果然输出10 20.
然后我又这样写了下:
<?php<br />
function test()<br />
{<br />
    $a=10;<br />
    global $a;<br />
    $b=$a+10;<br />
    global $b;   <br />
}<br />
test();<br />
echo $a."<br>";<br />
echo $b."<br>";<br />
?>
Copy after login

这次就蛋疼了,为什么不报错也不提示,而是输出的是空白呢?
既然上全局变量,整个php文件都能用吧,我又进行了下面的测试:
global $c;<br />
$c=7;<br />
function test()<br />
{<br />
    echo $c;   <br />
}<br />
test();<br />
Copy after login

然后提示,未定义变量.
是不是,定义了全局变量也不能在函数里面用?要想将值传给函数,只能通过参数的方式呢?
------解决方案--------------------
声明在前,使用在后
这是普遍的原则

global 是在函数中声明全局变量用的
------解决方案--------------------
先定义变量 $a='aaa';

函数中
function test()
{
global $a;
//使用$a;
}
------解决方案--------------------
第一个例子,你是先定义全局变量,然后使用全局变量。
第二个例子,你是先定义本地变量,然后再定义全局变量。但是你赋值的时候是使用本地变量,所以出现了你所见到的问题。
------解决方案--------------------
<?php<br />
function test()<br />
{<br />
    $a=10;      // 这个是局部变量以下同样<br />
    global $a;  // 这里声明了全局变量,以后出现的$a就是全局变量$a了<br />
                   // 在函数中使用全局变量都需要这样先声明一下否则默认为局部变量<br />
    $b=$a+10;<br />
    global $b;   <br />
}
Copy after login

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

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Huawei Watch GT 5 smartwatch gets update with new features Huawei Watch GT 5 smartwatch gets update with new features Oct 03, 2024 am 06:25 AM

Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

Tekken\'s Colonel Sanders dream fried by KFC Tekken\'s Colonel Sanders dream fried by KFC Oct 02, 2024 am 06:07 AM

Katsuhiro Harada, the Tekken series director, once seriously tried to bring Colonel Sanders into the iconic fighting game. In an interview with TheGamer, Harada revealed that he pitched the idea to KFC Japan, hoping to add the fast-food legend as a g

Cybertruck FSD reviews praise quick lane switching and full-screen visualizations Cybertruck FSD reviews praise quick lane switching and full-screen visualizations Oct 01, 2024 am 06:16 AM

Tesla is rolling out the latest Full Self-Driving (Supervised) version 12.5.5 and with it comes the promised Cybertruck FSD option at long last, ten months after the pickup went on sale with the feature included in the Foundation Series trim price. F

Garmin releases Adventure Racing activity improvements for multiple smartwatches via new update Garmin releases Adventure Racing activity improvements for multiple smartwatches via new update Oct 01, 2024 am 06:40 AM

Garmin is ending the month with a new set of stable updates for its latest high-end smartwatches. To recap, the company released System Software 11.64 to combat high battery drain across the Enduro 3, Fenix E and Fenix 8 (curr. $1,099.99 on Amazon).

New Xiaomi Mijia Graphene Oil Heater with HyperOS arrives New Xiaomi Mijia Graphene Oil Heater with HyperOS arrives Oct 02, 2024 pm 09:02 PM

Xiaomi will shortly launch the Mijia Graphene Oil Heater in China. The company recently ran a successful crowdfunding campaign for the smart home product, hosted on its Youpin platform. According to the page, the device has already started to ship to

First look: Leaked unboxing video of upcoming Anker Zolo 4-port 140W wall charger with display First look: Leaked unboxing video of upcoming Anker Zolo 4-port 140W wall charger with display Oct 01, 2024 am 06:32 AM

Earlier in September 2024, Anker's Zolo 140W charger was leaked, and it was a big deal since it was the first-ever wall charger with a display from the company. Now, a new unboxing video from Xiao Li TV on YouTube gives us a first-hand look at the hi

Samsung Galaxy Z Fold Special Edition revealed to land in late October as conflicting name emerges Samsung Galaxy Z Fold Special Edition revealed to land in late October as conflicting name emerges Oct 01, 2024 am 06:21 AM

The launch of Samsung's long-awaited 'Special Edition' foldable has taken another twist. In recent weeks, rumours about the so-called Galaxy Z Fold Special Edition went rather quiet. Instead, the focus has shifted to the Galaxy S25 series, including

Manjaro 24.1 \'Xahea\' launches with KDE Plasma 6.1.5, VirtualBox 7.1, and more Manjaro 24.1 \'Xahea\' launches with KDE Plasma 6.1.5, VirtualBox 7.1, and more Oct 02, 2024 am 06:06 AM

With a history of over one decade, Manjaro is regarded as one of the most user-friendly Linux distros suitable for both beginners and power users, being easy to install and use. Mostly developed in Austria, Germany, and France, this Arch-based distro

See all articles