|
-
Aug 29th, 2008, 04:21 PM
#1
Thread Starter
Addicted Member
[RESOLVED] What's wrong with this code?
I have this code that I copied from this link. 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:
PHP Code:
$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.
Changes are not permanent, but change is. {Neil Peart}
-
Aug 29th, 2008, 05:53 PM
#2
Re: What's wrong with this code?
You are having problems with the scalar ($), most likely, because PHP uses the scalar to denote a Variable.
My usual boring signature: Something
-
Aug 29th, 2008, 10:44 PM
#3
Re: What's wrong with this code?
$buf['user'] is not an array.
Use var_dump or print_r to display variables and their types.
PHP Code:
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.
Last edited by penagate; Aug 29th, 2008 at 10:47 PM.
-
Aug 30th, 2008, 03:29 AM
#4
Re: What's wrong with this code?
 Originally Posted by dclamp
You are having problems with the scalar ($), most likely, because PHP uses the scalar to denote a Variable.
What's a scalar?
-
Aug 30th, 2008, 01:36 PM
#5
Re: What's wrong with this code?
 Originally Posted by visualAd
What's a scalar? 
http://www.vbforums.com/showthread.php?t=473176
My usual boring signature: Something
-
Aug 30th, 2008, 04:00 PM
#6
Re: What's wrong with this code?
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.
-
Aug 30th, 2008, 10:59 PM
#7
Re: What's wrong with this code?
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.
-
Sep 2nd, 2008, 09:32 AM
#8
Thread Starter
Addicted Member
Re: What's wrong with this code?
 Originally Posted by penagate
$buf['user'] is not an array.
Use var_dump or print_r to display variables and their types.
PHP Code:
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.
Changes are not permanent, but change is. {Neil Peart}
-
Sep 5th, 2008, 03:23 PM
#9
Thread Starter
Addicted Member
Re: What's wrong with this code?
In the foreach() statement what is the purpose of the => ?
Changes are not permanent, but change is. {Neil Peart}
-
Sep 6th, 2008, 04:28 AM
#10
Re: What's wrong with this code?
The => operator separates the key and value in a key–value pair.
It can be used in associative array assignment:
PHP Code:
$ages = array( 'Kevin' => 50, 'Gordon' => 57, 'George' => 62 );
or in enumeration:
PHP Code:
foreach ($ages as $name => $age) echo $name.' is '.$age."\n";
Omitting this construct enumerates the values only:
PHP Code:
foreach ($ages as $age) echo $age."\n";
or you can use array_keys to enumerate the keys only:
PHP Code:
$names = array_keys($ages); foreach ($names as $name) echo $name."\n";
-
Sep 9th, 2008, 12:36 PM
#11
Thread Starter
Addicted Member
Re: What's wrong with this code?
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
PHP Code:
$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
PHP Code:
array_walk_recursive($ini, array('INI', 'replace_consts'), array());
return $ini;
Changes are not permanent, but change is. {Neil Peart}
-
Sep 9th, 2008, 07:37 PM
#12
Re: What's wrong with this code?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|