/    Sign up×
Community /Pin to ProfileBookmark

If not globals, what?

What’s the new way to create a configuration file that every function and file has access to? Stacking it in $_SESSION?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaNov 23.2004 — [i]Originally posted by subnet_rx [/i]

[B]What's the new way to create a configuration file that every function and file has access to? Stacking it in $_SESSION? [/B][/QUOTE]


[code=php]
// include configuration file
// assumed in the /inc/ directory
// change accordingly.
// The DOCUMENT_ROOT is just the path to
// your location on the server.
include($_SERVER["DOCUMENT_ROOT"]."/inc/config.cfg.php");
[/code]
Copy linkTweet thisAlerts:
@subnet_rxauthorNov 23.2004 — are functions called within that page going to have access to the variables, or do you have to include that on every PHP page?
Copy linkTweet thisAlerts:
@NogDogNov 23.2004 — [i]Originally posted by subnet_rx [/i]

[B]are functions called within that page going to have access to the variables, or do you have to include that on every PHP page? [/B][/QUOTE]

Whenever this sort of thing becomes an issue, it's always a good idea to take a step back and look at your code design. Global variables in any progamming language invariably become a maintenance nightmare. If a function needs to read a variable, make it an input parameter to that function instead. If a function needs to change a value, have it return a value instead and use it to change the value within your script.

Anyway, the official info on variable scoping is at http://us2.php.net/manual/en/language.variables.scope.php .
Copy linkTweet thisAlerts:
@JonaNov 23.2004 — [i]Originally posted by subnet_rx [/i]

[B]are functions called within that page going to have access to the variables, or do you have to include that on every PHP page? [/B][/QUOTE]


[font=trebuchet ms]Include the configuration file on every page you want access to configured variables; this way you can avoid using global variables. Moreover, you can maintain variables across multiple pages by file inclusion rather than having to use sessions (which I wouldn't recommend doing anyway). You can also use includes this way for a global set of functions accessible to every page that you want.[/font]
×

Success!

Help @subnet_rx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...