Home PHP Filesystem parse_ini_string() PHP parse_ini_string() Function Definition In PHP, parse_ini_string() reads settings written in INI format from a string and returns them as an array.
It works like parse_ini_file(), but you give it the text itself. This is handy when the settings come from a database, an API or a variable in your script.
Syntax Call parse_ini_string() with the values you need. Start with this line.
$config = parse_ini_string("name = CodeToFun"); Call What it does parse_ini_string('name = CodeToFun')Returns an array with one key, name. parse_ini_string($text, true)Keeps the [section] groups in the result. parse_ini_string($text, false, INI_SCANNER_RAW)Returns every value exactly as written.
The scanner modes work the same as in parse_ini_file(). In normal mode, on, yes and true become 1, and off, no, false and none become an empty string. If the text has a syntax error, PHP shows a warning and the function returns false.
Library declarations parse_ini_string(string $ini_string, bool $process_sections = false, int $scanner_mode = INI_SCANNER_NORMAL): array|false Parameters Parameter Description ini_string Required. The text in INI format. process_sections Optional. If true, a multidimensional array is returned using the [section] names. The default is false. scanner_mode Optional. INI_SCANNER_NORMAL (default), INI_SCANNER_RAW or INI_SCANNER_TYPED.
Return Value Return Value Description array|false Returns the settings as an associative array, or false on failure.
PHP Examples 1. Parse a Short INI String 2. Broken INI Text 3. Compare Normal and Raw Modes Example 1 — Parse a Short INI String Turn a few lines of text into an array.
<?php
$ini = '
name = CodeToFun
language = PHP
';
print_r(parse_ini_string($ini)); Array
(
[name] => CodeToFun
[language] => PHP
) How It Works $ini holds two lines in INI format. parse_ini_string() reads each name = value line. Blank lines and extra spaces around the equal sign are ignored. print_r() shows the array that was created. Example 2 — Broken INI Text A section that is never closed is a syntax error, so the function returns false.
<?php
$bad = '[broken section';
$result = @parse_ini_string($bad, true);
if ($result === false) {
echo "Invalid INI text", PHP_EOL;
} How It Works The bracket after the word broken is missing, so the text is not valid INI. parse_ini_string() raises a warning and returns false. The @ hides the warning for this demo. The strict check shows your own error message instead. Frequently Asked Questions What does parse_ini_string() do in PHP? It parses text in INI format and returns an associative array of settings.
When should I use parse_ini_string() instead of parse_ini_file()? Use it when the INI text is already in a variable, for example when it comes from a database or an API.
What does parse_ini_string() return on failure? It returns false and shows a warning. Always check the result with === false.
How do I get numbers and booleans as real types? Pass INI_SCANNER_TYPED as the third argument. Numbers become integers and true or false become booleans.
Why do I get an empty string for off or no? In normal mode, off, no, false and none become an empty string. Use INI_SCANNER_RAW to keep the original text.
Next: pathinfo() pathinfo() splits a file path into its parts: the folder, the file name, the extension and.
pathinfo() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion