Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isset() and PHP global variables

Regarding global variable initialization,

function hello_testing() {
    global $conditional_random;
    if (isset($conditional_random)) {
        echo "foo is inside";
    }
}

The global variable (conditional_random) may not be initialized before the hello_testing() function is called.

So, what happens to my validation via isset() when $conditional_random is not initialized? Will it fail or it will always be true?

like image 783
justjoe Avatar asked Apr 03 '10 14:04

justjoe


3 Answers

Well, why don't you just test ? ;-)

Note: It is not as easy as you'd think -- read the full answer ;-)


Calling the hello_testing(); function, without setting the variable:

hello_testing();

I get no output -- which indicates isset returned false.


Calling the function, after setting the variable:

$conditional_random = 'blah';
hello_testing();

I get an output:

foo is inside

Which indicates global works as expected, when the variable is set -- well, one should not have any doubt about that ^^



But note that isset will return false if a variable is set, and null!


See the manual page of isset()

Which means that a better test would be:

function hello_testing() {
    global $conditional_random;
    var_dump($conditional_random);
}

hello_testing();

And this displays:

null

No Notice: the variable exists! Even if null.

As I didn't set the variable outside of the function, it shows that global sets the variable -- but it doesn't put a value into it; which means it's null if not already set outside the function.


While:

function hello_testing() {
    //global $conditional_random;
    var_dump($conditional_random);
}

hello_testing();

Gives:

Notice: Undefined variable: conditional_random

It proves that notices are enabled ;-)

And, if global didn't "set" the variable, the previous example would have given the same notice.


And, finally:

function hello_testing() {
    global $conditional_random;
    var_dump($conditional_random);
}

$conditional_random = 'glop';
hello_testing();

Gives:

string 'glop' (length=4)

(This is to purely to demonstrate my example is not tricked ^^)

like image 185
Pascal MARTIN Avatar answered Oct 21 '22 14:10

Pascal MARTIN


You can check to see if the global has been created by checking if the key exists in $GLOBALS:

echo array_key_exists('fooBar', $GLOBALS) ? "true\n" : "false\n";
// Outputs false

global $fooBar;

echo array_key_exists('fooBar', $GLOBALS) ? "true\n" : "false\n";
// Outputs true

echo isset($fooBar) ? "true\n" : "false\n";
// Outputs false

This is the only way I know of to check the existence of a global without triggering a warning.

As Manos Dilaverakis mentioned, you should avoid using globals whenever possible.

like image 30
Omn Avatar answered Oct 21 '22 16:10

Omn


SOLUTION 1

No need to use any function. If a global variable is not set, it will result as null, and null is equal to false in PHP statements. So you can use something like this and it will work:

global $GLOBAL_VARIABLE;
if ($GLOBAL_VARIABLE) {
  // Global variable exists
} else  {
  // Global variable does not exists
}

SOLUTION 2

if (isset($GLOBALS["name"])) {
  // Global variable "name" exists
} else  {
  // Global variable does not exists
}
like image 2
Federico Schiocchet Avatar answered Oct 21 '22 15:10

Federico Schiocchet