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?

1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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?
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.