sublime代码错误提示_Sublime Text /代码解析错误无故,隐藏字符

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.

解决方案

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": " " } },

:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值