2

I have the following code:

$a='1';

function check() {
  global $a;

  if ($a == '1') {
    return "1";
  }
  else {
    return "2";
  }

  var_dump(check());
}

When I run this code stand alone, its output is string(1) "1"; when I use the code inside page-front.tpl.php the output is string(1) "2.

Why?

1 Answer 1

3

When Drupal includes the template file, your declaration of $a = '1' is not in the global scope. You need to declare it as such:

global $a;
$a = '1';

Since you're diving into the realm of global variables though, I'd recommend using a more distinctive name, or figuring out how to avoid globals altogether.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.