PHP Heredoc 解析错误

发布于 2024-08-01 13:43:21 字数 1350 浏览 2 评论 0原文

这会产生输出页面 OK

$mystring = "<<<EOT";

将其替换为以下内容会产生

解析错误:语法错误,file.php 第 737 行出现意外的 $end

$mystring = <<<EOT
   This is some PHP text.
   It is completely free
   I can use "double quotes"
   and 'single quotes',
   plus $variables too, which will
   be properly converted to their values,
   you can even type EOT, as long as it
   is not alone on a line, like this:
EOT;

关于导致解析器阻塞的任何想法?

我正在使用 PHP 4.4.7。

仅在一个文件上发生此行为,所有其他文件都遵循 PHP 定义的功能。

我试图重述的是程序行中可能出现的错误,以便 PHP 解析器显示此故障。

John

将文件内容更改为:-

<?php

$mystring = <<<WHATEVER
   This is some PHP text.
WHATEVER;
?>

结果 =

解析错误:语法错误,第 5 行 file.php 中的意外 $end

任何线索

编辑

原始错误与 T_ENCAPSED_AND_WHITESPACE 有关,例如这可能是由 jQuery 引起的“if(x == y){$('#my_image').hide():}” 位于定理文档中,二元组“{$ ​​将启动解析器寻找 php 变量进行替换。

编辑

2 个好的回复。

1) Ch4m3l3on - “”与“”处理

2) 分解器 - < /code> 一个愚蠢的程序也有类似的问题,该程序坚持将 BOM 放入 utf-8 文件中(忽略首选项)

编辑

1) 用单个块替换所有内容。没有解决问题或给出任何其他指示

2)没有BOM(字节顺序标记),遗憾的是这个或类似的majic字符可以完美地解释所有症状。

This produces output page OK

$mystring = "<<<EOT";

Replacing it with the following produces

Parse error: syntax error, unexpected $end in file.php on line 737

$mystring = <<<EOT
   This is some PHP text.
   It is completely free
   I can use "double quotes"
   and 'single quotes',
   plus $variables too, which will
   be properly converted to their values,
   you can even type EOT, as long as it
   is not alone on a line, like this:
EOT;

Any ideas as to what is causing the parser to choke?

I'm using PHP 4.4.7.

It is only on one file that this behaviour happens all others follow the PHP defined functionality.

What I am trying to recitify is what could be possibly wrong in the proceding lines so that the PHP parser shows in this failure.

John

changed file contents to :-

<?php

$mystring = <<<WHATEVER
   This is some PHP text.
WHATEVER;
?>

result =

Parse error: syntax error, unexpected $end in file.php on line 5

Any clues

EDIT

original error was to do with T_ENCAPSED_AND_WHITESPACE this can be caused with jQuery for example "if(x == y){$('#my_image').hide():}" is within the heredoc the bigram "{$ will start the parser looking for php variable for substitution.

EDIT

2 good responses.

1) Ch4m3l3on - "<?php" vs "<?" handling.

2) The Disintegrator - <q>had a similar problem with a stupid program that insisted in putting the BOM in a utf-8 file (ignoring preferences)</q>.

EDIT

1) Replacing all content with a single block didn't fix the problem or give any other pointers.

2) No BOM (Byte Order Mark), pity as this or similar majic characters would have explained all symptoms perfectly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小耗子 2024-08-08 13:43:22

我没有 uptic 的代表,但 Ch4m3l3on 和 Pedro Cunhna 是对的......不正确的定界符可能不会导致意外的 $end 错误,但未闭合的大括号肯定会导致。

I don't have the rep for an uptic, but Ch4m3l3on and Pedro Cunhna are right... an incorrect heredoc probably won't cause that unexpected $end error, but an unclosed curly brace definitely would.

满栀 2024-08-08 13:43:22

确保“WHATEVER;”后面没有任何内容。 即使是空格也会给出解析错误。
我会删除该行并重新输入,在输入分号后立即点击

Make sure that there is nothing after the 'WHATEVER;'. Even a space will give a parse error.
I would delete the line and retype it, hitting <enter> immediately after typing the semi-colon.

如果没结果 2024-08-08 13:43:22

确保 EOT; 确实位于该行的开头。

if ($muh="kuh") {
     $foo = <<<EOT
           some text text text
EOT;
}

make sure that EOT; is really at the begin of the line.

if ($muh="kuh") {
     $foo = <<<EOT
           some text text text
EOT;
}
高跟鞋的旋律 2024-08-08 13:43:22

如果您尝试怎么办:(

$mystring = <<<'EOT'
...
EOT;

请注意第一个 EOT 周围的单引号)

What if you try:

$mystring = <<<'EOT'
...
EOT;

(Notice the single quotes around the first EOT)

莫相离 2024-08-08 13:43:22

我只是将其复制/粘贴到一个文件中,它运行时没有错误。 (PHP 5.2.8 (cli) (built: Feb 6 2009 12:33:08))

所以问题可能是在该代码附近,而不是您在问题中包含的内容。

要么是这样,要么是自您的版本构建以来 PHP 发生了变化。

I just copy/pasted that into a file and it ran without errors. (PHP 5.2.8 (cli) (built: Feb 6 2009 12:33:08))

So the problem is likely something near that code, but not something you included in the question.

Either that, or a change in PHP since your version was built.

累赘 2024-08-08 13:43:22

您可能应该检查是否有任何未闭合的大括号(或大括号,不记得了)。

例如:

<?php

while(true) {
    echo "Something\n";

?>

会产生该错误。

You probably should check if you have any unclosed curly bracket (or brace, can't remember).

For example:

<?php

while(true) {
    echo "Something\n";

?>

Will produce that error.

戏剧牡丹亭 2024-08-08 13:43:21

您必须将结束heredoc放在行的开头。 如果您使用某些有缩进的 IDE,请将其删除! 你的结尾heredoc必须与你的结尾php标签垂直在同一行(

you have to place your ending heredoc at the beginning of line. if you use some IDE that have indentation, remove them! your ending heredoc must be vertically in the same line as your ending php tag(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文