Jump to content

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

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.