Knowledge Base

My PHP functions cannot reference global PHP variables

PHP used within a template is processed by the PHP function eval().  Because of this, variables in your PHP code are all considered to be of local scope, and your functions will not be able to reference them unless they are explicitly declared as globals.

Incorrect implementation:

<?php

$foo
= 'Hello World!';

bar();

function
bar() {
  
global $foo;
  echo
$foo;
}
?>

Correct implementation:

<?php

global $foo;
$foo = 'Hello World!';

bar();

function
bar() {
  
global $foo;
  echo
$foo;
}
?>

Last Updated on Jan 13, 2009 at 11:45   ( Permalink )
Category: Templates, PHP