I have a homepage where I have a number of variables such as a phone number, open times etc and I cant think of a way to store multiple variables permanently other than in a database and I dont want to make a table just to store 1 row's worth of properties. I was thinking of possibly using a text file but I dont really know.
This is exactly the kind of thing that CMS solutions such as Joomla are built for. As soon as you start going down this route with any decent amount of data you probably want to start looking at something like that.
However, if you really want to, you can go down the route of serialising whatever you want to store and putting it into a text file.
For example, setting it up:
$configuration = array( 'PhoneNumber' => '123456789',
'OpenTimes' => 'Monday 9:00 to 12:00' );
file_put_contents( 'configuration.txt', serialize( $configuration );
Getting it back out:
$configuration = unserialize( file_get_contents( 'configuration.txt' ) );
The file can contain much anything - string or objects or whatever. Just be aware that if you store objects you'll need to ensure that you may need to include their class definition before you re-load them. And if you store a resource (like a DB connection - I know, unlikely!), you'll need to reconnect or reopen the resource.
Alternatively - if it really is just a series of simple strings, you might want to look at ini
files.
http://php.net/manual/en/function.parse-ini-file.php
Let's say you have configuration.ini
file with the content:
[address_info]
phone_number = 123456789
open_times = Monday 9:00 to 12:00
You can then load it with:
$configuration = parse_ini_file("configuration.ini", true);
And then reference with:
$configuration['address_info']['phone_number']
You may use PHP's require_once
if you were to store the page information in another PHP file as variables.
This will act like a database (sort of).
If this was your PHP file with your variables (let's call it a.php
):
<?php
$phoneNumber = "1234 5678";
$openTimes = "09:00-17:00";
?>
You would use require_once("/a.php");
to utilize the variables in the file and use them like this:
<?php
require_once("/a.php");
echo "Phone Number: " . $phoneNumber;
?>
require_once
is an extremely useful feature of PHP, and can be used for many purposes.
Potential use examples of require_once
:
header
PHP filerequire_once
'ed file is situated in a secure non-public area of your website (decreases the chances of someone 'hijacking' your database)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With