打开花括号的PHP分析错误

打开花括号的PHP分析错误,php,debugging,Php,Debugging,我在运行下面的代码时遇到了这个不熟悉的错误。我看不出有什么问题 错误: 解析错误:语法错误,第2行/var/www/crawler/sources.php中出现意外的“{” <?php sourcelist($filename = '/var/resources/sources.list'){ if (is_readable($filename)){ $handle = fopen($filename, "r"); $contents = fread($handle, filesize(

我在运行下面的代码时遇到了这个不熟悉的错误。我看不出有什么问题

错误: 解析错误:语法错误,第2行/var/www/crawler/sources.php中出现意外的“{”

<?php
sourcelist($filename = '/var/resources/sources.list'){

if (is_readable($filename)){
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

return(json_decode($contents));

} else {
return NULL;
}

}



print_r(sourcesList());
?>


这正是我运行的代码片段。出什么问题了?

您试图定义函数,但错过了
函数
关键字:

sourcelist($filename = '/var/resources/sources.list'){
应该是

function sourcelist($filename = '/var/resources/sources.list'){

您缺少函数关键字:

function sourcelist($filename = '/var/resources/sources.list'){
你期待什么

sourcelist($filename = '/var/resources/sources.list'){

}
怎么办?
从未听说过该构造

可能您试图在此处定义一个
sourcelist
函数(其中一个参数具有默认值)。您忘了用
function
关键字启动函数声明。因此,您的代码以调用
sourcelist
函数开始,然后是一个块的开始(这在那里是不允许的)

第二行应为:

function sourcelist($filename = '/var/resources/sources.list'){
试试这个

<?php
$filename = "/var/resources/sources.list"

function sourcelist($filename){

     if (is_readable($filename)){
     $handle = fopen($filename, "r");
     $contents = fread($handle, filesize($filename));
     fclose($handle);
     return(json_decode($contents));
     } else {
     return NULL;
     }

}

print_r(sourcesList($filename));
?>


sourcelist()看起来像是一个带有一个可选参数的函数,如果是这种情况,则需要在其前面加上“function sourcelist($filename='/var/resources/sources.list'){”。在检查文件是否可读之前,您可能希望先检查文件是否存在。@GregK
是否可读
“说明文件是否存在且是否可读”()@lonesomeday我不知道它是否存在,我学到了一些新的东西:)谢谢!我以后在阅读文件时会用到它。@GregK你在阅读文档时学到的东西…;-)@lonesomeday刚刚看到你也住在圣奥尔本斯,机会有多大!嗯,现在我看到了答案,这是显而易见的