【发布时间】:2018-08-24 01:52:31
【问题描述】:
我有一个测试连接速度的脚本。当我将其移至另一台服务器时,我收到以下警告:
Warning: Creating default object from empty value in /home/speed/public_html/common.php on line 26
这是我的代码的摘录:
## 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
}
}
}
我目前运行的是 PHP 5.5,另一台服务器运行的是更新版本的 PHP。
【问题讨论】:
-
这不是错误,而是警告。它只是告诉你
$config->{$section}不存在,所以它在你创建$config->{$section}->{$var}时为你创建了它。在尝试向属性添加子对象之前,您应该确保属性存在。
标签: php