Mattis
Mattis

Reputation: 5096

Sublime Text / code parse error for no reason, hidden characters

I have had this problem for years, but not as frequently as today. This prompted me to ask this question once and for all.

Sometimes (today for almost every logic) my trivial PHP code fails with a parse error. Sometimes PHP is right, but way to often there is no mistake and when writing the exact same code again it works without whining. Copy and pasting the code does not work, it has to be typed again in full.

Today, I have meticulously typed down every character exactly as the line failing and then removing the old one, just to have the script working. For example:

1    foreach ($_POST['data_positions'] AS $k => $v) {
2    
3    }

threw a parse error on line 1. I bumped the code down to row 2 and wrote the exact same line 1 again (I check several times that every character is the same), then removing line 2, and it works. Another example:

1    if (is_numeric($k)) {
2    
3    }

also fails on row 1. When I retype it, it works. Copy and pasting the line again does not work.

I am 100% certain the code is fine, so it leads me to believe it's some kind of encoding issue and that certain parts of the code is attributed a bad encoding (like pasting formatted text into a email).

I'm currently using Sublime Text (but have had the same types of mistaken parse errors in Notepad++ and the old Homesite editor as well), running MAMP on OS X, but it has happened on my Windows desktop machine as well.

Does anyone have any clue what I can do? It's super annoying to write the code several times and having it fail on you randomly.

EDIT (reopened): I still can't get it to work properly. No matter if I paste it to Notepad or if I type a completely new file from scratch. I've checked all through the database to the server to my editor that they all use UFT-8. It's almost always IF-statements that break (sometimes I have to type them down 3 or 4 times before it works. Trivial stuff, like if ($a === 3 || $a === 6) {}.

EDIT (solved!): Scroll down for the solution, I answered the question myself.

Upvotes: 3

Views: 1206

Answers (3)

Nigel Alderton
Nigel Alderton

Reputation: 2376

I realize that you've already accepted your own answer but for future reference, I would not recommend saving your code in files encoded as UTF-8.

When writing PHP or any other code, I would just use use plain old ASCII. Using any other encoding may lead to unexpected things happening - as you have already discovered.

Even if the output produced by your code is a web page which you want to encode as UTF-8, use ASCII for your code files. The encoding of your code files and the encoding of the page produced by those code files are two different things.

Upvotes: 0

Mattis
Mattis

Reputation: 5096

I finally found the answer. I had to find out what I did wrong by trial and error.

So, if your code breaks due to hidden characters, if you find out that the character is "Â" when switching to ISO-8859-1 from UTF-8, if it's mostly happening in OSX and in multiple editors (both Sublime Text and Coda for me) this is probably why:

In many operating systems you will get a non-breaking space instad of a normal space when you press alt+space. Many programming parsers (PHP, Xcode for example) will fail when parsing that character.

To fix it in Sublime Text you need to edit Preferences/Key bindings - User and add the line:

{ "keys": ["alt+space"], "command": "insert_snippet", "args": {"contents": " " } },

Remember that the key bindings file is an array with objects, and you need to keep the , at the end if you have more entries. Remove it if it's the last one.

If you need to fix it in any other editor, just Google for "yourEditor disable alt space".

EDIT:

I also noticed the same behaviour with alt+shift+space, therefore I added another line to the keybinding config:

{ "keys": ["alt+shift+space"], "command": "insert_snippet", "args": {"contents": " " } },

:)

Upvotes: 3

Lucky Soni
Lucky Soni

Reputation: 6888

you are unknowingly pasting hidden characters which cause this parse error. Probably its the source of code from where you are copying that is including these hidden characters in your clipboard. Maybe you should use notepad to strip off these hidden characters:

  1. Copy code from source
  2. Paste it in notepad
  3. Copy from notepad
  4. Paste in your code editor of choice

Also this might be helpful as you use sublime text

Upvotes: 1

Related Questions