我已将以下内容放入服务器的 .htaccess 文件中:
Options +ExecCGI
AddType application/x-httpd-php .php .htm .html
AddHandler x-httpd-php5 .php .htm .html
我正在使用 GoDaddy 托管。
执行此操作后,我在如下一行中收到解析错误:
namespace {
错误是:
Parse error: syntax error, unexpected '{'
执行此操作时:
namespace Name {
,我收到此错误:Parse error: syntax error, unexpected T_STRING
顺便说一下,命名空间的使用来自第三方库,而不是我自己的代码。
我究竟做错了什么?
最佳答案
尽管下面所说的所有内容可能对您的情况有所帮助并且适用,但似乎问题已经在您的 .htaccess 文件中引入了 than 行:AddHandler x-httpd-php5 .php .htm .html
... 将旧版本的 PHP 设置为请求处理程序。换成...AddHandler x-httpd-php5-4 .php .htm .html
将命名空间声明移到 HTML 文件的顶部。报价the docs :
The only code construct allowed before a namespace declaration is the
declare
statement, for defining encoding of a source file. In addition, no non-PHP code may precede a namespace declaration, including extra whitespace.
<html>
<?php namespace MyProject; // fatal error
// namespace must be the first statement in the script
?>
请注意,当像这样使用时,您可以将 HTML 内容放在不同的命名空间之间(它只是应该位于顶部的第一条语句):
<?php
namespace One;
function foo(){}
?>
<p>Got some <?= __NAMESPACE__ ?> for ya!</p>
<?php
namespace Two;
function foo(){}
?>
(很容易看出技术上
<p>
属于第一个命名空间,但这并不重要)。
关于php - 允许服务器解析 html 文件中的 php 后,命名空间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32049968/