Jump to content

Get variable inside of function


bsmedia

Recommended Posts

I dont know why I'm having so many problems with this. I need to get a variable that defined in an included file inside of a function I've got this

 

Inside file.php I've got the $variable1 and $variable2 defined (they're dynamic/different to each user)

 

include('file.php');

 

function foo() {

GLOBAL $variable1, $variable2;

 

echo $variable1;

echo $variable2;

}

 

foo();

 

Yet it outputs nothing?

 

If I Include file.php right inside of the function it works however. Any idea?

 

Link to comment
https://forums.phpfreaks.com/topic/261049-get-variable-inside-of-function/
Share on other sites

If the page you call foo() has included the file, the global would still work although it's not neat.

Like xyph suggested, you should stick to functions and returns or even consider putting them into classes if necessary.

 

Classes are NOT a stand-in for functions.  Classes have a specific use.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.