0

考虑这个 PHP 脚本:

<?php
  $a = "ok";
  function foo() {
    global $a; print "[$a]";
  }
  foo();
?>

[ok]正如人们所期望的那样,它在使用 PHP 解释器运行时会打印出来。但它仅[]在 Drupal 页面中运行时才会打印。为了让它在 Drupal 中工作,我必须在变量声明之前添加另一个全局规范,因此:

<?php
  global $a; // WHY IS THIS NEEDED IN DRUPAL?
  $a = "ok";
  function foo() {
    global $a; print "[$a]";
  }
  foo();
?>
4

1 回答 1

1

Likely because Drupal includes the file inside a function:

function render() {
    include 'my_script.php';
}

That makes $a local to the function, not global.

于 2013-07-03T15:31:10.620 回答