Reputation: 33
I'm looking to find a way to make a config.php
file to setup the website easier. See below;
config.php -
#Community Name
$commname = "Community A",
#Community Phone Number
$commphone = "0123456789",
index.php -
<h1>Community Name: <?php echo($commname) ?></h1>
<h2>Phone Number: <?php echo($commphone) ?></h2>
contact.php (same setup, just a different page) -
<h1>Community Name: <?php echo($commname) ?></h1>
<h2>Phone Number: <?php echo($commphone) ?></h2>
I'm fairly new to PHP so I'm not sure if echo
is the best way to do it or not.
Thanks!
Upvotes: 1
Views: 62
Reputation: 2255
You could do this:
config.php
<?php
$comm = [
'name' => 'Community A',
'address' => 'Baker Street',
'phone' => '0123456789'
];
other PHP files
<?php
include 'config.php'; // Assuming that config.php is inside the same folder of the others
?>
<h1>Communities</h1>
<p>Name: <?=$comm['name']?></p>
<p>Address: <?=$comm['address']?></p>
<p>Phone: <?=$comm['phone']?></p>
I prefer to use an associative array (here $comm
), instead of single variables, because it makes the code more clear and neat (it's just my opinion).
Upvotes: 0
Reputation: 44
Ye so just create a config.php
<?php
$serverName = "Server Name";
?>
Then inside your other php files include the config file by doing
<?php
require_once(__DIR__ . "/config.php");
?>
Then where you want to use the variable do
<title> <?php echo $serverName; ?> </title>
Upvotes: 0
Reputation: 498
It's not hte best way but if it's working - why not?
For example you can use Composer and use Dotenv package to create .env
file in your root folder and add some variables like:
COMMUNITY_NAME=APP //APP_NAME
COMMUNITY_PHONE=88816259
and then you can add to your root page(router or something else) and all variables will be stored after loading page with this code:
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
All variables will be able in global varable $_ENV
echo $_ENV['COMMUNITY_NAME']; //APP_NAME
Upvotes: 1