PHP 解析错误(意外 T_IF)

PHP parse error (unexpected T_IF)

我似乎无法找到为什么我会收到

Parse error: syntax error, unexpected 'if' (T_IF) in /Applications/XAMPP/xamppfiles/htdocs/oop/index.php on line 8

我检查了是否缺少 semi-colons/parentheses/curly 个大括号,但找不到任何东西!这些文件还涉及其他一些文件。我也应该 post 他们吗?

(getInstance 是我的 db class)
中的静态方法 (countget 是我 db class 中的方法)

提前致谢!

<?php

require_once 'core/init.php';

$user = DB::getInstance()->get('users', array('username', '=', 'bob'));

if(!$user->count()) {
    echo 'No user';
} else {
    echo 'OK';
}

?>

init.php 看起来像这样:

<?php 

session_start();

$GLOBALS['config'] = array(

'mysql' => array(
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'db' => 'oop'),

'remember' => array(
    'cookie_name' => 'hash',
    'cookie_expiry' => 604800),

'session' => array(
    'session_name' => 'user')
);

spl_autoload_register(function($class) {
    require_once 'classes/' . $class . '.php';
});

require_once 'functions/sanitize.php';
?>

你的文件在末尾包含字节序列0xefbbbf(正好是U+FEFF的UTF-8编码,零宽不间断space)第 6 行:

$ hexdump -C index.txt
00000000  3c 3f 70 68 70 0a 0a 72  65 71 75 69 72 65 5f 6f  |<?php..require_o|
00000010  6e 63 65 20 27 63 6f 72  65 2f 69 6e 69 74 2e 70  |nce 'core/init.p|
00000020  68 70 27 3b 0a 0a 24 75  73 65 72 20 3d 20 44 42  |hp';..$user = DB|
00000030  3a 3a 67 65 74 49 6e 73  74 61 6e 63 65 28 29 3b  |::getInstance();|
00000040  0a 24 75 73 65 72 2d 3e  67 65 74 28 27 75 73 65  |.$user->get('use|
00000050  72 73 27 2c 20 61 72 72  61 79 28 27 75 73 65 72  |rs', array('user|
00000060  6e 61 6d 65 27 2c 20 27  3d 27 2c 20 27 62 6f 62  |name', '=', 'bob|
00000070  27 29 29 3b ef bb bf 0a  0a 69 66 28 21 24 75 73  |'));.....if(!$us|
00000080  65 72 2d 3e 63 6f 75 6e  74 28 29 29 20 7b 0a 09  |er->count()) {..|
00000090  65 63 68 6f 20 27 4e 6f  20 75 73 65 72 27 3b 0a  |echo 'No user';.|
000000a0  7d 20 65 6c 73 65 20 7b  0a 09 65 63 68 6f 20 27  |} else {..echo '|
000000b0  4f 4b 27 3b 0a 7d 0a 0a  3f 3e                    |OK';.}..?>|
000000ba

很可能是您的文本编辑器错误地添加了这样一个不间断的 space,因为它不理解您正在使用代码。

PHP 愉快地将它解析为一个(未定义的)常量的名称,因此当它遇到 if 构造(在常量后立即在语法上无效)时会抱怨。

从您的文件中删除此非中断 space。