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>
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.