Forgot Password / Register
Site Statistics
Total Members: 510
Total Tutorials: 242
Newsest User: Hp
Todays Unique Hits: 101
0 Users 2 Guests Online

Reading INI files with PHP

Another easy tutorial, but it can be useful to store information outside of a MySQL database!

First up, let's make the ini file:

1
2
3
4
5
6
[SOMETHINGHERE]
makesure=1
youinclude=23
thebrakets=345
[SOMETHINGELSE]
YES=12


Alright, you MUST save that as a .ini file. Now for the PHP.

1
2
3
4
5
<?php
$arr=parse_ini_file("Yourfile.INI",true);
echo $arr['SOMETHINGHERE']['youinclude']; // echos 23
echo $arr['SOMETHINGELSE']['YES']; // echos 12
?>


Just edit your .ini file as well as your .php file. Here is a full example:

Mystats.ini:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[HP]
AddPerLevel=140
AddPerStr=10
AddPerDef=0
AddPerMagi=0
AddPerSpeed=0
[MP]
AddPerLevel=220
AddPerStr=0
AddPerDef=0
AddPerMagi=10
AddPerSpeed=0
[SP]
AddPerLevel=330
AddPerStr=0
AddPerDef=0
AddPerMagi=0
AddPerSpeed=20


Now, I want to get AddPerLevel under MP.

1
2
3
4
<?php
$arr=parse_ini_file("mystats.INI",true);
echo $arr['MP']['AddPerLevel']; // echos 220
?>


It's a pretty simple tutorial, enjoy!
Diablosblizz
Views:
6719
Rating:
Posted on Wednesday 5th March 2008 at 03:41 AM
ShadowMage
ShadowMage
Also, my random outcome i got bored with

http://hrwp.zapto.org:6020/demo/index.php
Posted on Tuesday 1st January 2008 at 02:53 AM
Diablosblizz
Diablosblizz
That's true as well.
Posted on Monday 31st December 2007 at 06:44 PM
ShadowMage
ShadowMage
if you use fopen it'll open the whole file, though you could most likely use file(); then check which line contains the content you want to edit.
Posted on Monday 31st December 2007 at 04:37 PM
Diablosblizz
Diablosblizz
Zerocool, you could use the fopen function.
Posted on Monday 31st December 2007 at 02:07 PM
Dean
Dean
good tutorial.
Posted on Sunday 30th December 2007 at 10:00 PM
ShadowMage
ShadowMage
try looking here :) i think i saw a function/class that has a write function.

http://us2.php.net/parse_ini_file
Posted on Sunday 30th December 2007 at 09:00 PM
zerocool
zerocool
nice tut, but i have a little question.

In PHP i can insert values on this ini files?