本文介绍了PHP新手问题:网络的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一些全局变量来存储几件事情。
我试过这样的:

i'd like to set up some global variables for storing several things.i've tried it like this:

function init_web()
{
    $webname = "myweb";
    $web['webname'] = $webname;
    $web['server_root'] = $_SERVER['DOCUMENT_ROOT']."/$webname/";
    $web['lang']="en";
}

问题在于我无法在函数内部访问这些变量。
我试过使用 global $ web; 但没有帮助。

the problem is that i can't access those variables inside of functions ..i've tried using global $web; but didnt help.

它是全球性的?

感谢

thanks

推荐答案

通常的全局变量很糟糕的哭泣,这里的基础知识:

While you'll get the usual "global variables are bad" crying, here's the basics:

$web = array(); // define the var at the "top level" of the code tree, outside any functions/classes.
function init_web() {
    global $web; // make it visible in the function
    $web['lang'] = 'en'; // make some settings
}

基本上,您已经拥有它,但尚未定义函数外的变量。在函数内部说'全局'不会奇迹般地在函数之外创建一个函数 - 在你尝试将函数内部化为函数并更改/访问它的内容之前,它已经存在了。

basically, you had it, but hadn't defined the variable outside the function. Just saying 'global' within the function won't magically create one outside the function - it already has to exist before you try to "internalize" it to the function and change/access its contents.

这篇关于PHP新手问题:网络的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:17