Jump to content

Archived

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

phlai

Some questions~

Recommended Posts

Hi Guys,

Im new to php.. May i ask some questions?

If I want to centralize some of the common variables into a php file, and access them through it, may i know how?

Example..

Index.php      - Default page
Settings.php  - Common variables that can be reused by all the php files
                  - Eg. $Database_name, $Database_username, $Database_password
Category.php -Have a function to retrieve database information.

Index.php include both Settings.php, and category.php by:
    require_once("Settings.php");
    require_once("Category.php");

But when in Category.php i want to use the variable $Database_name i cannot access it..

Is there a offical way to store all the common variable in the common access php file?

Share this post


Link to post
Share on other sites
The way you have described your layout, and variables within Settings.php will be available within Category.php if called from within index.php.

If you want to call Category.php by itself and still access variables from within Settings.php, you will need to include Settings.php into Category.php.

[code=php:0]
require_once("Settings.php");
[/code]

Using require_once() will stop any errors that may occur from trying to include the same file multiple times.

Share this post


Link to post
Share on other sites
Just another information, you have to put the require_once before you try to use them.

Share this post


Link to post
Share on other sites

×

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.