php 语法错误,意外的 T_SL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5416437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error, unexpected T_SL
提问by Lance
I'm fairly new to php and I'm using a script that creates a function called the "mime_mailer" which essentially allows me to use PHP to send emails that are able to be designed with CSS instead of being merely plain text.
我对 php 还很陌生,我正在使用一个脚本来创建一个名为“mime_mailer”的函数,它基本上允许我使用 PHP 发送能够用 CSS 设计的电子邮件,而不仅仅是纯文本。
Yet, in my registration script, I try to write some code that sends a CSS email, but I get an error saying that there's a syntax error. Could someone please fill me in on this?
然而,在我的注册脚本中,我尝试编写一些发送 CSS 电子邮件的代码,但我收到一条错误消息,指出存在语法错误。有人可以帮我补充一下吗?
$subject = "Your Red-line Account";
$css = "body{ color: #090127; background-color: #f0f0f0; }";
$to = $usercheck;
//Message
$message =<<<END
<html>
<head>
<title>
Red-line
</title>
</head>
<body>
<p>
Hi $first_name,
</p>
<p>
Your Red-line account is almost complete. To finish, go to <a href='www.thered-line.com'>The Red-line</a> and enter your eight digit confirmation code.
</p>
<p>
Your confirmation code is: <b>$code</b>
</p>
<p>
Sincerely,
</p> <br />
<p>
The Red-line Operator
</p>
</body>
</html>
END;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "From: The Red-line <[email protected]>\r\n";
$headers .= "To: $first_name $last_name <$usercheck>\r\n";
// Mail it
require_once("function_mime_mailer.php");
mime_mailer($to, $subject, $message, $headers, NULL, $css);
}
Here is the code for the "function_mime_mailer.php" file.
这是“function_mime_mailer.php”文件的代码。
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404(); // stop http access to this file
function mime_mailer($to, $subject, $message, $headers = NULL, $attachments = NULL, $css = NULL)
{
if(!preg_match('/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a- z]{2,6})$/', $to)) return FALSE;
if(preg_match('/<(html|head|body|div|a|h|p|table|br|img|b|hr|ol|ul|span|pre|i|form)[^>]*[^>]*>/i', $message)) $html = TRUE;
if(stristr($message, '<body')) $message = stristr($message, '<body');
$message = delete_local_links($message);
if(empty($headers)){
$headers = "MIME-Version: 1.0\n";
}else{
$headers.= "\nMIME-Version: 1.0\n";
}
if(empty($html)){
$result = plain_text($message);
}elseif(isset($html) and $html == TRUE){
if(!isset($css)) $css = NULL;
if(preg_match('/<img[^>]+>/i', $message)){
$result = multipart_related($message, $css);
}else{
$result = multipart_alternative($message, $css);
}
}
$result['message'] = delete_non_cid_images($result['message']);
if(!empty($attachments)){
$parts = attachments($attachments);
array_unshift($parts, implode('', $result));
$result = multipart_mixed($parts);
}
$headers = $headers.$result['headers'];
//print '<pre>'.htmlspecialchars($headers.$result['message']).'</pre>';exit;
if(mail($to, $subject, $result['message'], $headers)) return TRUE;
return FALSE;
}
?>
回答by penner
Just had the same problem.
刚刚有同样的问题。
Turned out to be content on the same line as my opening HERDEOC
结果和我的开场HERDEOC在同一条线上
wrong example
错误的例子
echo <<<HEREDOC code started on this line
HEREDOC;
correct example
正确的例子
echo <<<HEREDOC
code should have started on this line
HEREDOC;
Hope this helps someone else!
希望这对其他人有帮助!
回答by Michiel Pater
Have a look at the List of Parser tokens.
查看解析器令牌列表。
T_SL
references to <<
.
T_SL
引用<<
.
You should not use tabs or spaces before you use END;
. Have a look at this huge warning.
在使用之前,不应使用制表符或空格END;
。看看这个巨大的警告。
回答by Kevin_Kinsey
A side note, but might well help someone: a bad git merge can cause this. Consider:
旁注,但可能对某人有所帮助:错误的 git merge 可能会导致这种情况。考虑:
function foo
<<<<<<< HEAD
$bar = 1;
<<<<<<< e0f2213bc34d43ef
$bar = 2;
The PHP parser would produce the same error.
PHP 解析器会产生同样的错误。
Source: just got bit by this ;)
来源:刚刚被这个 ;)
回答by Areeb Soo Yasir
It had the same exact same issue but mine was because I had whitespace at the end of my heredoc on the top line:
它有完全相同的问题,但我的问题是因为我的heredoc末尾有空格:
$var = <<< HTML(whitespaces here caused the error)
stuff in here
HTML;
HTML;
Source: http://realtechtalk.com/_syntax_error_unexpected_T_SL_in_PHP_Solution-2109-articles
来源:http: //realtechtalk.com/_syntax_error_unexpected_T_SL_in_PHP_Solution-2109-articles
回答by Costel Socianu
What version of php are you using? The nowdoc syntax is only valid since PHP 5.3.0. See the manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
你用的是什么版本的php?nowdoc 语法仅自 PHP 5.3.0 起有效。请参阅手册:http: //www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
回答by foens
There is a bug in function_mime_mailer.php:
function_mime_mailer.php 中有一个错误:
if(empty($headers)){
$headers = "MIME-Version: 1.0\n";
}else{
$headers.= "\nMIME-Version: 1.0\n";
}
should be
应该
if(empty($headers)){
$headers = "MIME-Version: 1.0\r\n";
}else{
$headers.= "\r\nMIME-Version: 1.0\r\n";
}
also, if you include the MIME-Version header, then the function will include it once more - effectively having two of them.
此外,如果您包含 MIME-Version 标头,则该函数将再次包含它 - 实际上有两个。