Click to See Complete Forum and Search --> : [RESOLVED] What's wrong with this code?
kzatu
Aug 29th, 2008, 04:21 PM
I have this code that I copied from this link (http://us3.php.net/manual/en/function.parse-ini-file.php#78221). It is a user made class to read/write ini files. I'm only using it because the parse_ini_file() seems to error out when the data contains a dollar sign ($) which my ini file has lots of.
This code works, but it generates a seemingly harmless error at the bottom of my php page which is annoying. The error I get is:Invalid argument supplied for foreach() in the READ INI section:$buf = get_defined_constants(true);
$consts = array();
foreach($buf['user'] as $key => $val) {
$consts['{'.$key.'}'] = $val;
}
array_walk_recursive($ini, array('INI', 'replace_consts'), $consts);
return $ini;
I'm a beginner at PHP and this specific foreach construct is a little new to me and I just don't know what is wrong with this code that would make it generate that error. I've tried my own hand at debugging and tweaking and anything I do just breaks the code. I would be willing to leave this code be if I could tell PHP to silence the error but I don't know how to do that without silencing all errors for the entire webserver.
Any help is much appreciated. :afrog:
dclamp
Aug 29th, 2008, 05:53 PM
You are having problems with the scalar ($), most likely, because PHP uses the scalar to denote a Variable.
penagate
Aug 29th, 2008, 10:44 PM
$buf['user'] is not an array.
Use var_dump or print_r to display variables and their types.
var_dump($buf);
You should never suppress error messages; in fact you should be doing precisely the opposite and cranking up the error reporting level in order to catch all bad code. Suppressing error messages is akin to pulling out the temperature warning light in a car engine — one day the engine will blow up without any warning because you ignored the problem.
visualAd
Aug 30th, 2008, 03:29 AM
You are having problems with the scalar ($), most likely, because PHP uses the scalar to denote a Variable.
What's a scalar? :confused:
dclamp
Aug 30th, 2008, 01:36 PM
What's a scalar? :confused:
http://www.vbforums.com/showthread.php?t=473176
visualAd
Aug 30th, 2008, 04:00 PM
Its not a scalar, its a dollar sign. In physics a scalar is a value which is only measured by its size (magnitude). The is_scalar function in PHP will provide you with the proper definition of scalar in programming; which appears to be synonymous with base type.
penagate
Aug 30th, 2008, 10:59 PM
A scalar is a value (e.g. 1, 1000) that is not a vector ([x, y]) or matrix ([[x1, y1], [x2, y2]]). The is_scalar function considers arrays, objects, and resources to be non-scalar, and everything else scalar. This is a definition which is particular to PHP, but in principle more or less in line with physics and other programming languages.
The '$' symbol is called a dollar sign.
This is also off-topic.
kzatu
Sep 2nd, 2008, 09:32 AM
$buf['user'] is not an array.
Use var_dump or print_r to display variables and their types.
var_dump($buf);
You should never suppress error messages; in fact you should be doing precisely the opposite and cranking up the error reporting level in order to catch all bad code. Suppressing error messages is akin to pulling out the temperature warning light in a car engine — one day the engine will blow up without any warning because you ignored the problem.
I have used print_r on all kinds of variables to try and figure this out. When I print_r on the $buf variable I see hundreds of variables, but the 'user' variable isn't one of them. This would support what you are saying about it not being an array. But for some reason the code works well enough to read my ini file the way it is--even though it generates an error.
kzatu
Sep 5th, 2008, 03:23 PM
In the foreach() statement what is the purpose of the => ?
penagate
Sep 6th, 2008, 04:28 AM
The => operator separates the key and value in a key–value pair.
It can be used in associative array assignment:
$ages = array(
'Kevin' => 50,
'Gordon' => 57,
'George' => 62
);
or in enumeration:
foreach ($ages as $name => $age)
echo $name.' is '.$age."\n";
Omitting this construct enumerates the values only:
foreach ($ages as $age)
echo $age."\n";
or you can use array_keys to enumerate the keys only:
$names = array_keys($ages);
foreach ($names as $name)
echo $name."\n";
kzatu
Sep 9th, 2008, 12:36 PM
Alright. That makes sense, and I can see how that would be very helpful somewhere. And your information somehow (not sure how) helped me fix this problem although indirectly.
I've learned that the $buf['user'] array does not exist in the get_defined_constants. I don't know how the original author of this code intended its use. On my system it simply doesn't exist. I believe this is why I was getting the Invalid Argument in the foreach() statement.
Because of the error, the code in the foreach() loop was not even getting executed. Leaving $consts to be an empty array.
I read about the array_walk_recursiveon php.net and I still don't understand the function of the third parameter. But since it appears to be an optional paramenter, I tried to run it without it but it malformed the data that it returned. So I put the third parameter back in but I replaced it with "array()" and now everything works without the Invalid Argument errors.
Here is what the before and after code looks like. Same data return, error free.
Before $buf = get_defined_constants(true);
$consts = array();
foreach($buf['user'] as $key => $val) {
$consts['{'.$key.'}'] = $val;
}
array_walk_recursive($ini, array('INI', 'replace_consts'), $consts);
return $ini;
After array_walk_recursive($ini, array('INI', 'replace_consts'), array());
return $ini;
penagate
Sep 9th, 2008, 07:37 PM
The INI::replace_consts function defined in that linked comment in your first post takes three parameters. The third parameter of array_walk_recursive specifies an arbitrary value which, if specified, is passed as the third parameter to the callback function (INI::replace_consts). If you were to omit the third parameter you would need to also remove it from the replace_consts function declaration.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.