关于全局变量global懂得和疑惑
关于全局变量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 /> ?>
恩,效果很好,果然输出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 /> ?>
这次就蛋疼了,为什么不报错也不提示,而是输出的是空白呢?
既然上全局变量,整个php文件都能用吧,我又进行了下面的测试:
global $c;<br /> $c=7;<br /> function test()<br /> {<br /> echo $c; <br /> }<br /> test();<br />
然后提示,未定义变量.
是不是,定义了全局变量也不能在函数里面用?要想将值传给函数,只能通过参数的方式呢?
------解决方案--------------------
声明在前,使用在后
这是普遍的原则
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 /> }

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

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











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

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

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 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).

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

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

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

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
