3

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';
?>
tereško
  • 58,060
  • 25
  • 98
  • 150
user3794832
  • 117
  • 1
  • 1
  • 12
  • 3
    Are you positively sure that there isn't a syntax error in `core/init.php`? Do you get any parse errors after navigating to that page by itself? – Crackertastic Mar 09 '15 at 00:05
  • Yeah I've been starting at this code snippet for 5 minutes and can't see anything wrong with it. – mittmemo Mar 09 '15 at 00:07
  • Me neither, I can't see any syntax errors in the snippet, which is what is leading me to believe that it is in the required file, or even before that. – Crackertastic Mar 09 '15 at 00:08
  • First off, why are you using Windows (no offense intended)? It may be that UAC and other traditional woes associated with running a web server on Windows may be affecting your PHP... – Sameer Puri Mar 09 '15 at 00:09
  • No matter how many times I count, that `if` is always appearing on line 7 not line 8. So you've definitely omitted to show *something* that could be relevant. – eggyal Mar 09 '15 at 00:09
  • 3
    @flyingeagle413 That file path indicates that OP is using OS X... – mittmemo Mar 09 '15 at 00:12
  • 2
    @flyingeagle413 UAC in Windows isn't going to trigger a parse error if the syntax is valid. – Crackertastic Mar 09 '15 at 00:14
  • @mittmemo You're right, I jumped on the fact that it was XAMPP and assumed it was Windows >.< yeah there must be something missing from this... – Sameer Puri Mar 09 '15 at 00:18
  • Also, @eggyal, I removed an extra blank line while formatting, sorry about that – user3794832 Mar 09 '15 at 00:21
  • Are you *sure* that line is actually blank? Perhaps it contains some non-printing character? – eggyal Mar 09 '15 at 00:23
  • There aren't any parse errors in your `init.php` file either..... _however_ .....you do include another file, `functions/sanitize.php` that could contain the parse error. – Crackertastic Mar 09 '15 at 00:23
  • sorry for that formatting up there, but thats my sanitize function – user3794832 Mar 09 '15 at 00:25
  • 1
    No parse errors on sanitize, init, or dp.php-- only in index.php – user3794832 Mar 09 '15 at 00:27
  • 1
    It may be worth having a look at index.php with a hex editor. At least you will know that there are no unexpected characters in the file. – Ryan Vincent Mar 09 '15 at 00:32
  • i dont even know what a hex editor is, but i highlighted all the characters in sublime and it highlights all spaces in a different color, i've checked several times over, it seems like nothing is wrong with index :/ – user3794832 Mar 09 '15 at 00:49
  • Can you post the index.php file somewhere that we can download it? Perhaps rename it to index.txt and stick it on a webserver? – eggyal Mar 09 '15 at 00:53
  • https://www.dropbox.com/s/zcelkgtk50y6gsi/index.txt?dl=0 – user3794832 Mar 09 '15 at 00:58

1 Answers1

5

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.

eggyal
  • 122,705
  • 18
  • 212
  • 237
  • I had to completely remove that line before the if statement. What could've happened? and why did this never happen to me before? – user3794832 Mar 09 '15 at 02:42