Why does pasting a line break the code if writing out the same line by hand works fine?
Asked Answered
P

3

5

Here are two versions of a line in a php file:

First version:

if ($projet['sourceDonnees'] === (string)$CONSTANTS['sourceDonnees_saisie']) {

Second version:

if ($projet['sourceDonnees'] === (string)$CONSTANTS['sourceDonnees_saisie']) {

Although they look identical, the first version results in a PHP Parse error: syntax error, unexpected T_STRING, whereas the second version works fine. The difference between the two is that the first version was pasted in and modified whereas the second version was written out by hand entirely. What's going on here?

Notes: The line was copied from a text file encoded in UTF-8 and pasted into another UTF-8 text file. All operations done within gedit, both files written by me in gedit.

Percutaneous answered 22/3, 2013 at 14:49 Comment(7)
If your editor has the option to show symbols, try turning that on and see if tit helps shed some light.Showplace
I use BBEdit which lets me turn on the option to see invisible characters. Usually code that I find online and is copied from a browser is totally corrupted with invisible junk. It's best to re-type code you find online if your editor is not advanced enough to show you the stray characters. In your code, BBEdit is showing me one invisible character just between the ] and the ===, like this ]•===. Retype it with a space.Monteria
@Monteria How does one usually type that character? Maybe I typed it in by accident in the original string...Percutaneous
I don't think it's typed. I think the spaces and tabs get converted somehow when the original author publishes it to his blog or web page. Then you accidentally pick them up with a simple cut & paste. Your second line of code was perfectly clean.Monteria
The original author is myself, and the original file is a text file manipulated with gedit (no blog or web page)... That's why I'm so perplexed by this particular occurence of the problem!Percutaneous
I can't explain how you did it (I'd like to know too), but I can clearly see it in the first line and not in the second. Let me know if you can reproduce.Monteria
Further experimentation shows this invisible character as "non-ASCII"... BBEdit calls them "gremlins".Monteria
M
3

When I copied & pasted your first line into my text editor and turned on the "show invisible characters" option, it looked like this:

enter image description here

if ($projet['sourceDonnees']•=== (string)$CONSTANTS['sourceDonnees_saisie']) {

Notice the between the ] and the ===.

Your second line of code showed perfectly clean.

Many times you will pick up stray invisible characters when you copy & paste text from websites. However, I do not know what keyboard combination will reproduce this from scratch.

Further experimentation reveals this invisible character as "non-ASCII"... the BBEdit text editor simply calls them "gremlins", and even has a "zap gremlins" function.

Monteria answered 22/3, 2013 at 15:1 Comment(0)
C
5

You've copied UTF-8 quote marks, which are not parse-able by PHP. Remove the quote marks and replace them with ASCII equivalents (i.e. by typing them).

For more information on ASCII vs UTF-8 quote marks see http://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html

Censorship answered 22/3, 2013 at 14:51 Comment(2)
Really? Both lines were written by me using the same quotes in the same text editor...Percutaneous
I can't tell you how it would have happened to your file in particular, but I can pretty much guarantee that the quote marks have been converted to their unicode equivalent (having had it happen to me a few times before, normally copying from blogs). You can use xxd to check the hex codes if you really want to get deep into it...Censorship
M
3

When I copied & pasted your first line into my text editor and turned on the "show invisible characters" option, it looked like this:

enter image description here

if ($projet['sourceDonnees']•=== (string)$CONSTANTS['sourceDonnees_saisie']) {

Notice the between the ] and the ===.

Your second line of code showed perfectly clean.

Many times you will pick up stray invisible characters when you copy & paste text from websites. However, I do not know what keyboard combination will reproduce this from scratch.

Further experimentation reveals this invisible character as "non-ASCII"... the BBEdit text editor simply calls them "gremlins", and even has a "zap gremlins" function.

Monteria answered 22/3, 2013 at 15:1 Comment(0)
A
0

unexpected T_STRING is usually an issue with your quotes or tick marks. The 's in the code that you copied are UTF-8 and probably a version of the quote that PHP cannot parse, like the backticks we use for inline code here on SO. Try changing them to a regular single quote and it'll likely solve your issue.

If that's not the case, make sure you didn't miss the semicolon at the end of your function. This can cause the same error.

Annabelle answered 22/3, 2013 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.