【问题标题】:Unexpected character in input: '\' (ASCII=92) state=1输入中的意外字符:'\' (ASCII=92) state=1
【发布时间】:2013-06-13 22:08:08
【问题描述】:

我的客户说他在使用我的脚本时遇到了这个错误:

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /path/to//header.php  on line 34
Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in/path/to/header.php  on line 34

header.php 中的第 34 行就是 use \Main\Class;

现在,我告诉他他必须拥有PHP >= 5.3.0,他说他的 PHP 版本是 5.3.24

可能是什么问题?

编辑:之前和之后的行

30. // Define absolute path
31. define("ABSPATH", $abs_path);
32. $_SESSION["abs_path"] = ABSPATH;
33. 
34. use \CNS\main\CNS;
35. $cns = new CNS();

编辑 2:

他给我发了这个:

Program     Version
Apache:     2.2.24
CentOS:     CentOS release 6.4 (Final)
cPanel:     11.36.1 (build 8)
Curl:       7.12.1
MySQL       5.5.30
phpMyAdmin  3.5.5
Python:     2.6.6
Program     Version
Perl:       5.8.8
**PHP:        5.3.24**
ionCube Loader:     4.2.2
Zend Optimizer:     3.3.9
Ruby:       1.8.7
Rails:      3.2.8
OpenSSL:    1.0.0-fips

【问题讨论】:

  • @Jessica - 34 实际上。 ;-)
  • 我在第 34 行之前和之后添加了行
  • @OP 我认为您可能必须向我们展示您的完整代码,如果可能的话,除非Jessica 可以在没有它的情况下解决这个问题。
  • @Fred He 向我们展示了第 34 行,我要求查看第 33 行。
  • @Fred 之前没有太多,只是检查是否有一个变量说脚本已安装,在这段代码之后html开始。

标签: php namespaces


【解决方案1】:

就像其他用户说的那样:使用命名空间只对大于 5.3.0 的 PHP 版本有效,所以我能够包含使用命名空间的库的可选使用的解决方案是检查 php 版本 并使用 eval() 函数来避免较低版本的 PHP 出现错误,即使在编译时也是如此。

类似这样的:

if ( phpversion() > '5.3.0' ){
    include_once('/path/to/Library.php'); 
    eval("Library\Foo::bar();"); 
}

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,做了一些研究,我设法解决了它。 就我而言,我使用 PHP7,我需要做的是编辑位于 ~/.composer/vendor/laravel/installer/ 的文件 laravel,其中 shebang 行是 #!/usr/bin/env php 我更改为 #!/usr/bin/env php7

    再次运行工匠后,我得到了它的工作:

    -bash-3.2$ laravel
    Laravel Installer version 1.3.3
    
    Usage:
      command [options] [arguments]
    
    Options:
      -h, --help            Display this help message
      -q, --quiet           Do not output any message
      -V, --version         Display this application version
          --ansi            Force ANSI output
          --no-ansi         Disable ANSI output
      -n, --no-interaction  Do not ask any interactive question
      -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    Available commands:
      help  Displays help for a command
      list  Lists commands
      new   Create a new Laravel application.
    

    【讨论】:

    • 实际上我正在寻找这个谢谢你解释得很好(y)
    【解决方案3】:

    如果您在提到的错误后收到“unexpected T_STRING”错误,则需要安装 PHP 5.4+

    【讨论】:

      【解决方案4】:
      <FilesMatch "\.(inc|php|php3|php4|php44|php5|php52|php53|php54|php55|php56|phtml|phps)$">
      
       AddHandler x-httpd-php53 .php
      
      </FilesMatch>
      

      在 .htaccess 中

      【讨论】:

        【解决方案5】:

        请他用phpinfo() 创建一个文件。他可能没有 PHP 版本 >= 5.3.0

        【讨论】:

          【解决方案6】:

          现在,我告诉他他的 PHP >= 5.3.0,他说他的 PHP 版本是 5.3.24

          可能是什么问题?

          不管他是否知道,他的 PHP 版本实际上是

          See the error occurring on many PHP versions.

          【讨论】:

          • 好吧,他告诉我现在是 5.3.24。当我要求他时,他没有使用phpinfo() 创建文件,但他复制了一些统计数据(可能来自 cpanel 或其他东西)。我将他的回复作为我的编辑 2 发布在我的帖子中。
          【解决方案7】:

          如果您尝试使用命名空间但没有 PHP 5.3,则会发生这种情况。 PHP 5.2 及以下版本不支持命名空间,当它们看到反斜杠时会抛出此错误。

          -- 编辑:混合版本。如果我没记错的话,它是 5.2 及以下版本没有命名空间。

          【讨论】:

          • 5.3 确实支持命名空间,你的意思可能是 5.2 及以下
          • 事实不准确使答案不是很有用,即使它们是小脑屁。 +1 修复它。
          • @jraede ,我目前有 php 5.1。有什么解决办法吗?对于 php 5.1 的版本?
          猜你喜欢
          • 2014-07-23
          • 2012-10-18
          • 2015-12-24
          • 2011-12-11
          • 2013-09-14
          • 1970-01-01
          • 2019-01-05
          • 2019-08-17
          • 2019-06-25
          相关资源
          最近更新 更多