0

I have tried reading parameters from php ini files using parse_ini_file

Here is my code

$param_array=parse_ini_file("test.ini");
print $param_array[0];
print $param_array[1];

Above code returns nothing

test.ini file contains,

[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3schools.com"

Now i need to call the Robert & Peter

2
  • 1
    It should be $param_array["me"] instead of $param_array[0]. I can only second @popnoodles, use PHP's Manual instead, it's reliable and there is plenty of examples too. Commented Jan 13, 2013 at 14:06
  • Do this if (!$param_array=parse_ini_file("test.ini")) echo 'nope'; and print_r($param_array); and show us what you get Commented Jan 13, 2013 at 14:09

3 Answers 3

2

If you'd have rather consulted the Manual Pages instead of w3fools, you'd have seen a reliable example where it's shown that the returned array is associative, that is, the names of the properties are the keys of the array.
So instead of

$param_array[0]

you should use

$param_array['me']

to access "Robert"

Sign up to request clarification or add additional context in comments.

Comments

1

parse_ini_file returns an associative array. You are trying to access the fields in your array using numerical indices, but they aren't defined.

Try accessing those fields using the names you gave them in the ini-file as the key:

echo $param_array['me']; // will yield Robert

Helpful tip: investigate the structure of arrays and variables to find out what they look like using print_r or var_dump

Comments

0

Following code worked!

print $param_array["me"];
print $param_array["you"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.