parse_ini_file() error

I’m trying to catch the case where I’m opening an .INI file that doesn’t exist.

$iniFile = "phpfun.ini";
try {
	$ini = parse_ini_file($iniFile, true);
}
catch (Exception $e) {
	die('Missing INI file: ' . $iniFile);
}
if ($ini == false)
	die('Missing INI file: ' . $iniFile);
echo "{" . $ini["mysql"]["database_server"] . "}";

This code works fine when the phpfun.ini file is in place; it’s when I remove it that it does something unexpected. It displays in the browser the following warning:

( ! ) Warning: parse_ini_file(phpfun.ini) [function.parse-ini-file]: failed to open stream: No such file or directory in L:\var\www\phpfun\iniparse.php on line 25

The documentation for this function states that it returns FALSE on failure, which it does, but it also prints this warning.

Return Values

The settings are returned as an associative array on success, and FALSE on failure.

I added the try/catch block in order to handle this error on my own, but the warning persists. How should this be done so my program can handle this warning without it being displayed?

Add an “@” in front of
parse_ini_file(
So
@parse_ini_file(

Okay, I changed my code to:

try {
	if (($ini = @parse_ini_file($iniFile, true)) == false)
		throw new Exception('Missing INI file: ' . $iniFile);
}
catch (Exception $e) {
	die($e->getMessage());
}

…and that works (thank you), but why? I’ve come across a lot of weird things in Joomla code, trying to learn PHP. I’ve taken the w3schools PHP tutorial, but never figured out what ‘@’ does. It is very difficult to do a Google search for meta characters, like ‘@’. So, can you point me to a language reference that explains this for the beginner, please?

Here’s the manual page.

Don’t ever do that. :rolleyes:

use file_exists to check if it exists before reading it.

Surpressing errors with the @ is not a good idea. The error comes about because PHP can’t find a file, in the case of the script it can’t find the phpfun.ini file. Have you checked that it exists? Try specifying the whole path to the file.

1 Like

And the manual states that track_errors is OFF my default. I wonder why?

I’m like an inquisitive little kid today.

Thanks again!

Maybe I didn’t make it clear. I can open the INI file fine. I just want my code to be more robust. I’m going to read my on error handling:

http://www.php.net/manual/en/ref.errorfunc.php