Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load variables from external file in PHP

Tags:

How can I import a variable from an external file? What I want to do is to have a configuration file in which I can write all my website settings and then to import these settings to every file, so I can set the website skin and things like that.

How can I do this?

like image 603
Adrian M. Avatar asked Apr 09 '10 15:04

Adrian M.


People also ask

How do I assign one variable to another in PHP?

PHP variables: Assigning by Reference PHP (from PHP4) offers another way to assign values to variables: assign by reference. This means that the new variable simply points the original variable. Changes to the new variable affect the original, and vice a verse.

How do I load a PHP file?

php" file extension. Open up any Web browser on your desktop and enter "localhost" into the address box. The browser will open a list of files stored under the "HTDocs" folder on your computer. Click on the link to a PHP file and open it to run a script.


2 Answers

Look at this :

http://php.net/manual/en/function.parse-ini-file.php

you'll be happy :)

like image 167
remi bourgarel Avatar answered Oct 10 '22 01:10

remi bourgarel


You can have a file with configuration and then include it on every script, like jeroen told you:

config.inc.php

$config['dbname'] = 'myDB';
$config['dbuser'] = 'user';

...

then in your scripts

include_once('config.inc.php');

You could also use inheritance where you have a model for example that uses the config and then you can extend that model class.

like image 45
mandril Avatar answered Oct 10 '22 02:10

mandril