【问题标题】:PHP: "unexpected $end" in the middle of a filePHP:文件中间出现“意外的$end”
【发布时间】:2010-05-05 15:40:39
【问题描述】:

当我尝试运行此页面 (video.php) 时,我收到以下错误:

解析错误:语法错误,第 37 行 /base/path/masked/inc/functions.php 中的意外 $end

奇怪的是“functions.php”有超过 37 行......为什么它检测到文件结尾?我不认为我缺少任何括号或括号,因为每个函数只有一个语句(打印语句)长。

我已经做了几件事来尝试解决这个问题。如果我删除 print_head() 和 print_foot() 的函数定义中的语句,错误就会消失(页面的其余部分工作正常)。如果我删除任一函数中的语句,我会得到相同的错误,只是在不同的行上。如果我在页面上移动函数定义,我会得到同样的错误。我什至尝试删除部分 print 语句,但仍然出现同样的错误。

编辑:
'videos/transfer/playlist' 是 get_vids() 加载的示例文件。它是一个包含偶数行的平面 txt 文件;奇数行是视频文件的名称,偶数行是前一个文件的标题。我已经测试以确保 get_vids() 按预期工作。

编辑:
这是我尝试从命令行运行所有内容时得到的结果:

$ php -l video.php
No syntax errors detected in video.php
$ php video.php

Parse error: syntax error, unexpected $end in /home/nova20/http-dir/orientation/inc/functions.php on line 37
$ php -l inc/functions.php

Parse error: syntax error, unexpected $end in inc/functions.php on line 37
Errors parsing inc/functions.php

这是我的代码:

video.php:

<?php
include('inc/functions.php');

$type=$_GET['type'];
if($type == '') {
    $type = 'transfer';
}

$vidno = $_GET['vid'];
if($vidno == '') {
    $vidno = 1;
}

$vidindex = $vidno - 1;

$videos = get_vids($type);

$filename = $videos[$vidindex]['file'];
$title = $videos[$vidindex]['title'];
$basedir = "videos/$type";
$vidfile = "$basedir/$filename";

if($vidfile != '') {
    $extra = '<script src="/flowplayer/flowplayer-3.1.4.min.js"></script>';
    print_head($title, $extra);

    print <<<ENDHTML
<p>
<a
    href='$vidfile'
    style="display:block;width:640px;height:498px;"
    id="player"
></a>
</p>

<p id="contlink" style="display:none">
<a href="done.php?type=$type&vid=$vidno">Click Here to continue</a>
</p>

<script language="JavaScript">
    flowplayer(
        "player",
        "/flowplayer/flowplayer-3.1.5.swf",
        {
            clip: {
                onFinish: function(){
                    //window.location = "done.php";
                    //alert('done!');
                    document.getElementById('contlink').style.display = "block";
                }
            },
            plugins: {
                controls: {
                    play:true,
                    volume:true,
                    mute:true,
                    time:true,
                    stop:true,
                    fullscreen:true,
                    scrubber:false
                }
            }
        }
    );
</script>
ENDHTML;

    print_foot();
} else {
    print_head('OOPS!');

    print <<<ENDERROR
<h1>OOPS!</h1>
<p>
It looks like there's no video here.  <a onclick="history.go(-1);return false;" href="#">Go back</a> and try again.
</p>
ENDERROR;

    print_foot();
}
?>

inc/functions.php(我认为问题出在哪里):

<?php
function get_vids($type) {
    $base = "videos/$type";
    $playlist = "$base/playlist";

    $vidinfo = file($playlist);

    $videos = array();

    for($i = 0; $i < count($vidinfo); $i += 2) {
        $filename = trim($vidinfo[$i]);
        $title = trim($vidinfo[$i+1]);

        if($filename != '') {
            $index = $i / 2;
            $video['file'] = $filename;
            $video['title'] = $title;

            $videos[$index] = $video;
        }
    }

    return($videos);
}

function print_head($title, $extra = '') {
    print <<<ENDHEAD
<html>
<head>
<title>$title</title>
$extra
</head>
<body>

ENDHEAD;
}

function print_foot() {
    print <<<ENDFOOT

</body>
</html>
ENDFOOT;
}
?>

视频/传输/播放列表

1.flv
Introduction
2.flv
Why am I doing this?
3.flv
What can I access with RAIN?
4.flv
How do I access my RAIN Account?
5.flv
How do I Check my registration status?
6.flv
Evaluating transfer credit
7.flv
Transferable degrees
8.flv
Physical Education and History
9.flv
Regents exemptions
10.flv
Academic status
11.flv
How to find my academic advisor?
12.flv
Is Financial Aid available?
13.flv
How do I check my financial aid status?
14.flv
How do I transfer my hope scholarship?
15.flv
Payment information
16.flv
Student Services (Part 1)
17.flv
Student Services (Part 2)
18.flv
Student Services (Part 3)
19.flv
Campus Bookstore
20.flv
Where can I eat on Campus?
21.flv
Where can I live on Campus?
22.flv
How do I register for Parking?
23.flv
Still Have questions?

【问题讨论】:

  • 这段代码的格式和你使用的一样吗?
  • 格式是复制粘贴的。我在问题描述中添加了“视频/传输/播放列表”。

标签: php printing unexpectendoffile


【解决方案1】:

它本身并不是检测文件结束,而是检测可执行代码行的逻辑结束。

确保您的 HEREDOC 结束标记(ENDHEAD;和 ENDFOOT;)前面没有空格 - the moment they're not the first token on the line, they don't register as HEREDOC end tokens but as an arbitrary string within,这样您的 HEREDOC 会占用更多的代码块。

这是我唯一想到的——php -l &lt;your functions.php&gt; 没有给我带来任何错误(但在 ENDHEAD 之前添加了一个空格;给了我你描述的错误)。

【讨论】:

  • 关闭,但没有雪茄。我忘了提到我尝试将 heredoc 块拆分为单独的打印语句并得到相同的错误。
  • 好吧,不过,您粘贴的内容检查正常。如,将其粘贴到本地文件中并执行php -l 会产生No syntax errors detected [...](在两个文件上)。尝试将您放在这里的内容粘贴回本地文件中,看看是否可以解决问题?可能是某个隐藏的损坏字符(例如,半个 UTF-8 字符)触发了它。
  • php -l inc/functions.php 给了我语法错误。我编辑了帖子以反映这一点。
  • 即使您将放在这里的代码粘贴回空白文件并用它替换inc/functions.php,它仍然会发生? o.O
  • 问题已解决。我从这个页面复制了functions.php到我的functions.php的实际副本(并用标签替换了“”的出现),它工作得很好。也许我的编辑在某个地方放了一个奇怪的角色。我感觉像一个ID-10T。感谢您的帮助。
【解决方案2】:

我已经为你修复了代码:

    <?php
include('inc/functions.php');

$type=$_GET['type'];
if($type == '') {
    $type = 'transfer';
}

$vidno = $_GET['vid'];
if($vidno == '') {
    $vidno = 1;
}

$vidindex = $vidno - 1;

$videos = get_vids($type);

$filename = $videos[$vidindex]['file'];
$title = $videos[$vidindex]['title'];
$basedir = "videos/$type";
$vidfile = "$basedir/$filename";

if($vidfile != '') {
    $extra = '<script src="/flowplayer/flowplayer-3.1.4.min.js"></script>';
    print_head($title, $extra);

    ?>
<p>
<a
    href='<?=$vidfile;?>'
    style="display:block;width:640px;height:498px;"
    id="player"
></a>
</p>

<p id="contlink" style="display:none">
<a href="done.php?type=<?=$type;?>&vid=<?=$vidno;?>">Click Here to continue</a>
</p>

<script language="JavaScript">
    flowplayer(
        "player",
        "/flowplayer/flowplayer-3.1.5.swf",
        {
            clip: {
                onFinish: function(){
                    //window.location = "done.php";
                    //alert('done!');
                    document.getElementById('contlink').style.display = "block";
                }
            },
            plugins: {
                controls: {
                    play:true,
                    volume:true,
                    mute:true,
                    time:true,
                    stop:true,
                    fullscreen:true,
                    scrubber:false
                }
            }
        }
    );
</script>
<?php

    print_foot();
} else {
    print_head('OOPS!');

?>
<h1>OOPS!</h1>
<p>
It looks like there's no video here.  <a onclick="history.go(-1);return false;" href="#">Go back</a> and try again.
</p>
<?php

    print_foot();
}
?>

您可以在要显示的 html 周围打开和关闭 php 标签 - 正如您将在上面看到的那样:)

希望有帮助

【讨论】:

  • '那些奇怪的“打印
  • HEREDOC 不会导致问题;我认为 pinkgothic 是在正确的轨道上。
  • "Heredoc 是一种在 PHP 中使用更多行但不使用引号创建字符串的强大方法。Heredoc 很少使用,因为日常使用比使用引号或双引号创建字符串更复杂。此外这个未正确使用的 heredoc 可能会导致您的代码出现问题。” ...这就是我没有遇到它的原因,然后:D
  • 这有时可能有效,但并不总是有效。例如,我希望 php 在“print_head”中评估 $title 和 $extra 的值,而不是实际打印出“$title”和“$extra”。 Heredocs 允许我只输入这些变量,而无需添加额外的 php 标签和打印语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-20
相关资源
最近更新 更多