Reputation: 11
I have a script which tests the connection speed. When I moved it to another server I got the following warning:
Warning: Creating default object from empty value in /home/speed/public_html/common.php on line 26
Here is an excerpt from my code:
## Read through the config file and assign items to the global $config variable
function ReadConfig($config_file) {
global $config;
$lines = file($config_file);
foreach ($lines as $line_num => $line) {
$line = rtrim(preg_replace("/#.*/","",$line));
if(preg_match("/\[.*\]/", $line, $parts)) {
$section = $parts[0];
$section = preg_replace("/[\[\]]/","",$section);
} elseif (preg_match("/=/",$line)) {
list($var,$value) = split('=',$line);
$var = preg_replace('/ $/','',$var);
$value = preg_replace('/^ +/','',$value);
$config->{$section}->{$var} = $value; # here
}
}
}
I am currently running PHP 5.5, the other server runs a newer version of PHP.
Upvotes: 1
Views: 465
Reputation: 10734
@Sjon provides the answer to get rid of the warning, I will just explain why you see the warning now.
Since you moved your code to another server, there is most probably another php ini file and thus, different settings. On your "old" server, you had the errors
and warnings
most likely switched off so you did not see them, on the "new" server they are switched on by default.
Instead of displaying errors you can log them so you do not see them while browsing:
display_errors(false);
// you definitely wanna log any occurring
log_errors(true);
Upvotes: 1
Reputation: 5165
Prefix line 26 with this check:
if (!isset($config->{$section}))
$config->{$section} = new Stdclass;
and it should work without generating warning
Upvotes: 2