dtdvlazd56637 2015-03-08 23:58
浏览 72
已采纳

PHP解析错误(意外T_IF)[重复]

This question already has an answer here:

I can't seem to find why I'm getting a

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

I checked for missing semi-colons/parentheses/curly braces but can't find anything! There are a few more other files these are referring to. Should I post them as well?

(getInstance is a static method in my db class)
(count and get are methods in my db class)

Thanks in advance!

<?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 looks like this:

<?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';
?>
</div>
  • 写回答

1条回答 默认 最新

  • dongliang1654 2015-03-09 01:08
    关注

    Your file contains the byte sequence 0xefbbbf (which happens to be the UTF-8 encoding of U+FEFF, the zero-width non-breaking space) at the end of line 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
    

    Most likely your text editor erroneously added such a non-breaking space because it didn't understand that you were working with code.

    PHP happily parses it as the name of a(n undefined) constant, and consequently complains when it then encounters an if construct (which is syntactically invalid immediately after a constant).

    Remove this non-breaking space from your file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀