PHP Heredoc 解析错误
这会产生输出页面 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我没有 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.
确保“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.确保 EOT; 确实位于该行的开头。
make sure that EOT; is really at the begin of the line.
如果您尝试怎么办:(
请注意第一个
EOT
周围的单引号)What if you try:
(Notice the single quotes around the first
EOT
)我只是将其复制/粘贴到一个文件中,它运行时没有错误。 (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.
您可能应该检查是否有任何未闭合的大括号(或大括号,不记得了)。
例如:
会产生该错误。
You probably should check if you have any unclosed curly bracket (or brace, can't remember).
For example:
Will produce that error.
您必须将结束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(