Php strpos()不';不行?

Php strpos()不';不行?,php,filesystems,Php,Filesystems,我正在玩弄一个php文件系统: 我的php文件: <html> <head> <title>Text Database Editor</title> <style type="text/css"> div.comment { color:green; } </style> </head> <body> <?php function interpret() { for ($coun

我正在玩弄一个php文件系统:

我的php文件:

<html>
<head>
<title>Text Database Editor</title>
<style type="text/css">
div.comment {
    color:green;
}
</style>
</head>
<body>
<?php
function interpret() {
    for ($count=1;!empty($fileline[$count]);$count++) {
        if (strpos($fileline[$count],"//")===0) {
            $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
        }
    }
}
$filepath = "data.bdf";
//in bdf files, each line starts with ` and commented lines begin with `//
$filesize = @filesize($filepath);
if (!empty($filesize)) {
echo "File being opened contains ".$filesize." bytes of data. Opening file...<br />";
}
else {
    echo "Error in determining file size. ";
}
$handle = @fopen($filepath, "r") or die("File could not be opened");
echo "File Opened!<br />";
$filedata = fread($handle, $filesize+1) or die("File could not be read");
echo "File Read!<br /><br />Data in file:<br /><br />";
$fileline = explode("`",$filedata);
interpret();
for ($count=1;!empty($fileline[$count]);$count++) {
    echo $count.": ".$fileline[$count]."<br />";
}
?>
</body>
</html>
正如您可能知道的,我正在阅读bdf文件,并试图在屏幕上显示所有注释(在`被删除后以//开头的行)时将它们变成绿色。这并没有发生,但为什么?我认为这是一个问题:

$fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
$fileline[$count]=”.$fileline[$count]。”;
html输出为:

<html>
<head>
<title>Text Database Editor</title>
<style type="text/css">
div.comment {
    color:green;
}
</style>
</head>
<body>
File being opened contains 44 bytes of data. Opening file...<br />File Opened!<br />File Read!<br /><br />Data in file:<br /><br />1: //This is a comment
<br />2: This is not a comment<br /></body>
</html>

文本数据库编辑器
部门意见{
颜色:绿色;
}
正在打开的文件包含44字节的数据。正在打开文件…
文件已打开
文件读取

文件中的数据:

1://这是一条注释
2:这不是评论

非常感谢您提前提供帮助

您的函数
exploration()
引用了
$fileline
,它是一个全局变量,但不使用
全局
关键字

相反,将
$fileline
作为参数传递给
explorate()

// Argument reference &$fileline
function interpret(&$fileline) {
  for ($count=1;!empty($fileline[$count]);$count++) {
    if (strpos($fileline[$count],"//")===0) {
        $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
    }
  }
}

// Later, your function call...
$fileline = explode("`",$filedata);
interpret($fileline);

函数
explorate()
引用了
$fileline
,这是一个全局变量,但不使用
global
关键字

相反,将
$fileline
作为参数传递给
explorate()

// Argument reference &$fileline
function interpret(&$fileline) {
  for ($count=1;!empty($fileline[$count]);$count++) {
    if (strpos($fileline[$count],"//")===0) {
        $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
    }
  }
}

// Later, your function call...
$fileline = explode("`",$filedata);
interpret($fileline);

@Ben7005是的,正如我在稍后的编辑中所说。重新定义您的函数以需要
$fileline
参数,并按照您的猜测调用它。@实际上,您需要将变量作为引用传递:
函数解释(&$fileline){
;否则,调用该函数时,函数外部的实际
$fileline
将保持不变。@Michael:您可能忘记了“按引用传递”或“返回”语句。@Ben7005即使您如上所述重新定义了函数,它也不起作用。@Michael:您的示例同时使用了
返回
和按引用传递时间。-@Ben7005是的,正如我在稍后的编辑中所说的。重新定义您的函数以需要
$fileline
参数,并根据您的猜测调用它。@Michael实际上,您需要将变量作为引用传递:
函数解释(&$fileline){
;否则,调用该函数时,函数外部的实际
$fileline
将保持不变。@Michael:您可能忘记了“按引用传递”或“返回”语句。@Ben7005即使您如上所述重新定义了函数,它也不起作用。@Michael:您的示例同时使用了
返回
和按引用传递编辑:在这里显示:他删除了CSS标签。1)-你需要把你的$FLILLIN信息传递给函数解释()。另外,考虑使用FrACH循环。这里显示的是编辑:他删除了CSS标签。1)-你需要把你的$FLILLIN信息传递给函数解释()。此外,2)考虑使用Frach循环代替。
function interpret($fileline) {
  for ($count=1;!empty($fileline[$count]);$count++) {
    if (strpos($fileline[$count],"//")===0) {
        $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
    }
  }
  // Return the argument instead
  return $fileline;
}

// Later, your function call...
$fileline = explode("`",$filedata);
$fileline = interpret($fileline);