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

Comments

enorp’s picture

try this

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

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

that works! Thank you