0

I've got two isset function

function getPlayers(){
if (isset($_POST['select'])) 
{
    global $t1select;
    global $t2select;

The code above is part of the first function, notice the two global variables I declared, I did this because I would like to use them in my second functions:

function PlayerAttributes(){
if (isset($_POST['teamselect'])) {

The function above is my second function.

The Problem

When I try to refer to the global variables in the second function, I get the error message "Undefined variable: t1select "

What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Marilee
  • 1,598
  • 4
  • 22
  • 52

2 Answers2

1

You have to put global $var in every function you wish to use them in, not just one.

Collin Grady
  • 2,226
  • 1
  • 14
  • 14
1

Varibles should be declared as global in every function that is going to use them. Otherwise they will be only for the local scope of the function.

Another approach is to use the $GLOBALS['varname'] syntax. This will work without any declarations.

bbonev
  • 1,406
  • 2
  • 16
  • 33
  • Hi thank you for your answer. The error is not showing up anymore, but nothing gets displayed. When I do a var_dump on the variable it returns null, however when I do a var_dump on the variable without declaring it as a global I get the correct contents, any idea what going on here? – Marilee Feb 09 '14 at 04:14
  • I notice you declare the global variables after some `if` statement - in this case the variable will not be `global` in statements before the declaration. – bbonev Feb 09 '14 at 04:18