Reputation: 2794
Properties prop = new Properties();
prop.load(new FileInputStream("my.properties"));
prop.setProperty("name", "my name");
prop.getProperty("name");
This looks so simple to set and get properties in Java. I searched for same thing in PHP, but didn't find anything useful related to this.
I've read somewhere that
PHP can natively load and parse .ini files using
parse_ini_file()
.
As I just have started learning PHP, I can't find any code that can read write key/value pairs from .ini files in a way as simple as we do in Java with .properties files. How might I do this?
Upvotes: 1
Views: 6448
Reputation: 6950
Try this ...
ini files in php contains your configurations .....
the sample file ini will something look like .
[first_section]
db = testdb
host = myhost
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
Now you may get the contents of this file by
<?php
$config_array = parse_ini_file('sample.ini');
echo "<pre>";
print_r($config_array);
echo "</pre>";
?>
The output will be something like
Array
(
[db] => testdb
[host] => myhost
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
you can also get the section names by passing second argument as 'TRUE' in parse_ini_file
$config_array = parse_ini_file('sample.ini',true);
then output will be
Array
(
[first_section] => Array
(
[db] => testdb
[host] => myhost
)
[second_section] => Array
(
[path] => /usr/local/bin
[URL] => http://www.example.com/~username
)
)
You can also save the confuguration in array as use it in later stage as suggested by kingkero...
Read more here http://www.php.net/manual/en/function.parse-ini-file.php#78221
Upvotes: 4
Reputation: 10638
In PHP I think it is more common to use files that return an array with configuration (see php.net/include)
config.php
<?php
return array(
'key1' => 'val1',
//etc.
);
other file:
<?php
$config = include 'config.php';
Upvotes: 1