Php 为什么我的$setting数组变量不保留其值?

Php 为什么我的$setting数组变量不保留其值?,php,zend-framework,Php,Zend Framework,我有以下问题:- //index.php <?php define('PATH','Ajfit/'); /* Get all the required files and functions */ require(PATH . 'settings.inc.php'); require(PATH . 'inc/common.inc.php'); ?> //setting.inc.php <?php $settings['language']='English'

我有以下问题:-

//index.php
<?php

define('PATH','Ajfit/');

/* Get all the required files and functions */
require(PATH . 'settings.inc.php');
require(PATH . 'inc/common.inc.php');

?>

//setting.inc.php
<?php
      $settings['language']='English';
?>

//inc/common.inc.php
<?php
      //global $settings, $lang, $_SESSION; //$setting = null????
      $language = $settings['language']; //$language is still null
?>
//index.php
//setting.inc.php
//inc/common.inc.php
当我尝试在common.inc.php中访问全局变量$settings时,它被设置为null,即使我在setting.inc.php中设置了该变量。如果我调试,当我退出setting.inc.php时,$settings valiable在index.php中设置,但是当我进入common.inc.php时,$settings valiable设置为null


有人有什么想法吗?

回答:
inc/common.inc.php
文件中,您不需要使用
global
关键字,变量已经可以访问了。使用
global
重新定义变量,从而使
null

说明:

//foo.php
  $foo = "bar";

  echo $foo; //prints "bar" since scope hasn't changed.

  function zoo() {
    echo $foo; //prints "" because of scope change.
  }

  function zoo2() {
    global $foo;
    echo $foo; //prints "bar" because $foo is recognized as in a higher scope.
  }

  include('bar.php');

//bar.php
  echo $foo; //prints "bar" because scope still hasn't changed.

  function zoo3() {
    echo $foo; //prints "" for the same reason as in zoo()
  }

  function zoo4() {
    global $foo;
    echo $foo; //prints "bar" for the same reason as in zoo2()
  }
变量范围是这里的关键。只有在范围更改时才需要使用
global
关键字。常规文件(包括
include()
s)的作用域都是相同的,因此相同作用域中的任何php都可以访问所有变量,即使它来自不同的文件

函数内部有一个需要使用
global
的示例。函数的作用域不同于普通php,后者不同于
class
scope等等

示例:

//foo.php
  $foo = "bar";

  echo $foo; //prints "bar" since scope hasn't changed.

  function zoo() {
    echo $foo; //prints "" because of scope change.
  }

  function zoo2() {
    global $foo;
    echo $foo; //prints "bar" because $foo is recognized as in a higher scope.
  }

  include('bar.php');

//bar.php
  echo $foo; //prints "bar" because scope still hasn't changed.

  function zoo3() {
    echo $foo; //prints "" for the same reason as in zoo()
  }

  function zoo4() {
    global $foo;
    echo $foo; //prints "bar" for the same reason as in zoo2()
  }
更多信息:

//foo.php
  $foo = "bar";

  echo $foo; //prints "bar" since scope hasn't changed.

  function zoo() {
    echo $foo; //prints "" because of scope change.
  }

  function zoo2() {
    global $foo;
    echo $foo; //prints "bar" because $foo is recognized as in a higher scope.
  }

  include('bar.php');

//bar.php
  echo $foo; //prints "bar" because scope still hasn't changed.

  function zoo3() {
    echo $foo; //prints "" for the same reason as in zoo()
  }

  function zoo4() {
    global $foo;
    echo $foo; //prints "bar" for the same reason as in zoo2()
  }

如果您想了解有关何时使用
global
和何时不使用
global的更多信息,请查看。

等等,为什么要在此处使用
global
,您已经在全局范围内编码了确切地说,您不需要全局关键字<代码>回显$settings['language']
将打印正确的值。请检查何时以及如何使用
global
:我不明白你的意思?如果我删除了global,则代码不会编译,因为$settings在common.inc.phpOK中未初始化,我理解,但我已经完全从common.inc.php中删除了全局行,并将其替换为$language=$settings['language'];但是,$language仍然为null,$settings也为null。请尝试使用
$settings=array()初始化数组$设置['language']='English'
这没有帮助,有趣的是,当我走出settings.inc.php时,$settings被填充,然后当我进入inc/common.inc.php时,$setting变量为null。它好像失去了它的范围。