php PHP解析错误:语法错误,意外的T_STRING,期待T_FUNCTION

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1288837/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:53:37  来源:igfitidea点击:

PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION

phpparsing

提问by Tyler Carter

I get this error in my PHP code:

我的 PHP 代码中出现此错误:

PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\Inetpub\wwwroot\webroot\www.novotempo.org.br\lib\Twitter.php on line 54

PHP 解析错误:语法错误,意外的 T_STRING,在第 54 行的 C:\Inetpub\wwwroot\webroot\www.novotempo.org.br\lib\Twitter.php 中需要 T_FUNCTION

The line in question:

有问题的行:

define('DEBUG',false);

Searching the net I found that this usually occurs when you′re using PHP 4.xx, but I′m using 5.2.6 (Just checked it using phpinfo()).

在网上搜了一下,发现这通常发生在你使用 PHP 4.xx 时,但我使用的是 5.2.6(只是使用 进行了检查phpinfo())。

I tried locally, and in two other external hosts, but it keeps returning the same message.

我在本地和另外两个外部主机上尝试过,但它一直返回相同的消息。

Why does this happen? How can I fix it?

为什么会发生这种情况?我该如何解决?

回答by Tyler Carter

If you are trying to DEFINEsomething inside of a class but outside of a function, you are going to get this error.

如果您尝试DEFINE在类内部但在函数外部进行某些操作,则会出现此错误。

(Normally the only place PHP will be looking for a function and not expecting a string is in a class, outside of a method)

(通常情况下,PHP 唯一会寻找函数而不期待字符串的地方是在类中,在方法之外)

IE: Your code should notlook like this:

IE:您的代码应该不会是这样的:

class myClass
{
    define("DEBUG", true);
    function myFunc()
     {
     }
}

回答by Lewis

Thanks for this, resolved an annoying issue quite quickly for me.

谢谢你,很快为我解决了一个烦人的问题。

I'd also like to add that the use of hyphens in constant names doesn't work and is apparently expected behaviour. It seems as though the echo tries to evaluate mathematics (minus) on the words.

我还想补充一点,在常量名称中使用连字符不起作用,显然是预期的行为。似乎回声试图评估单词的数学(减号)。

<?php
define('THIS-IS-A-TEST','Testing');
echo THIS-IS-A-TEST;
?>

Returns '0'

返回“0”

<?php
define('THIS_IS_A_TEST','Testing');
echo THIS_IS_A_TEST;
?>

Returns 'Testing'

返回“测试”