如何在drupal中进行全局工作

This snippet works as expected when run as a normal php web page.

<?php
  $foo = 123;

function whatever ()
{
  global $foo;
  echo "FOO = $foo<br/>";
}

whatever ();
?>

The output is

FOO = 123

But putting the exact same code in a drupal basic page gives this output

FOO = 

PHP is obviously working, but globals does not. I'm a noob with drupal, but I know php. I have googled this problem but found nothing relevant.

Why is it not working in drupal? How do I make it work? Please don't answer with, you should not use global!

Further experiments showed that this code in a Drupal php page works.

<?php
  global $foo;  // This trick makes it work
  $foo = 123;

function whatever ()
{
  global $foo;
  echo "FOO = $foo<br/>";
}
whatever ();
?>

The first "global" statement should not be needed, but seems to make it work. Also changing the first two lines to

$GLOBAL ["foo"] = 123;

is working.

Hope this can be helpful.