Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.