php global variables in drupal

helavissa - July 2, 2009 - 20:38

Hello,

I have a question (hopefully easy one) - how do I access a php global variable inside a function in Drupal?
For example, following code prints "Hello World", but why in Drupal it doesn't work?

<?php

    $test
= "Hello World";
   
    function
printVar(){
        global
$test;
        echo
$test;
    }
   
   
printVar();
?>

Please help,
Thank you

that's not a drupal prob :P

enorp - July 2, 2009 - 21:27

try this

global $test;
$test = "hello world";

function printVar(){
  //print $GLOBALS['test'];
  global $test;
  echo $test;
}
printVar();

that works! Thank you

helavissa - July 6, 2009 - 14:41

that works! Thank you

 
 

Drupal is a registered trademark of Dries Buytaert.