Jump to content

Some questions~


phlai

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?
Link to comment
https://forums.phpfreaks.com/topic/15582-some-questions~/
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.
Link to comment
https://forums.phpfreaks.com/topic/15582-some-questions~/#findComment-63382
Share on other sites

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.