1

I have a rather large set of global variables that I want to be able to use within other functions across various files. I know that to use these variables within other functions I need to use the global keyword to include the variable names.

Everything works when I do that, but since there are so many variables, I'd rather create a function that allows me to include the global declaration within the function, with the specific chunk of variables I need being included using a function parameter.

For illustrative purposes, this works:

variables.php (declared before other files are included)

$var1 = 'abc';
$var2 = 'def';
$var3 = 'ghi';

other-function-file.php

function my_function() {
  global $var1, $var2, $var3;
  return $var1 . $var2 . $var3;
}

But this does not work:

variables.php

$var1 = 'abc';
$var2 = 'def';
$var3 = 'ghi';

globals-list-declaration.php

global $var1, $var2, $var3;

globals-include-function.php

function include_globals() {
  include_once('globals-list-declaration.php');
}

other-function-file.php

function my_function() {
  include_globals();
  return $var1 . $var2 . $var3;
}

This is a little simplified, as there multiple variable and global declaration files, and I'll be able to specify which file to include by passing in a parameter to include_globals() in globals-include-function.php.

But for some reason, when I move the hard-typed global $var1, $var2, $var3; to a seperate file and include it in the exact same location (either through include or the include_globals() function decribed above), it ceases to work.

How can I make this work or is it even possible?

Thanks!

2
  • Are you including your variables.php, and how and when are you doing that? Commented Jan 19, 2018 at 18:19
  • @Progrock yes, upfront before the other stuff. As @FelippeDuarte Pointed out, it was a scope issue. Doing an include of the globals-list-declaration.php file directly rather than through another function solved the issue. Thanks! Commented Jan 19, 2018 at 19:14

2 Answers 2

2

From the docs http://php.net/manual/en/function.include.php:

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function

In other words, if can't use this:

function include_globals() {
    include_once('globals-list-declaration.php');
}

Because the definition of globals will only exists inside this function.

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

2 Comments

I see. I guess I technically could modify include_globals() to take the global variable values and store them in an array then use the array to create a parameter-specified output as needed? But I think this may be overly complicated for my purposes. The whole point was to be able to simplify things in the first place, haha. Thanks!
Or just use include('globals-list-declaration.php'); instead of a function. That will behave the way you expect.
0

Works for me:

<?php

$foo = 'hello';
$bar = 'world';

function include_globals()
{
    include 'do-globals.inc.php';

    return $foo . $bar;
}

echo include_globals();

Contents of do-globals.inc.php:

<?php

global $foo, $bar;

Output:

helloworld

Comments

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.