Stec Records Forum

Tools to make music

News: Note that we check all new user requests for spam violations. If you believe you have been rejected unfairly please contact us and we will attempt to resolve it with you. - Stecrew

Editing PHP files in Visual Studio (Read 5564 times)

  • Administrator
  • Hero Member
  • bob
  • Posts: 773
  • Karma: +0/-0
  • Time reveals truth.
    • Bob Sellon's Home Page
#1 Editing PHP files in Visual Studio
July 05, 2013, 02:53:26 PM
If you are using Microsoft's Visual Studio to edit PHP files, beware of the "Encoding" settings of the file which can cause extra characters to be added outside of the php tags.

Visual Studio keeps track of information about each file it opens including the Encoding which can indicate that the file should be saved "with signature" which adds a few "signature" bytes onto the beginning of the file. Not really something you want happening with php files since anything outside the php tags is considered html. In my case I was using the JQuery getJSON() function but was getting "parsererror, SyntaxError: JSON.parse: unexpected character" in the .fail() handler (showing up in the Firebug Script debugger).

After effectively removing all content from the php files involved I found I was still getting the error with just the php open and close tags.  So effectively empty files were producing an "unexpected character". Nice. As if that wasn't weird enough, the problem only occurred when I was doing a require_once inside a php file, and this is the rub, with a particular file. I could do a require_once with a different file and also no longer get the error. Everything was pointing to this file with just a php open and close tag. There was something there I couldn't see.

When I expanded the Firebug viewer for the entry to show the data returned by the PHP it looked blank and when JSON data was being passed it looked valid. When I checked the JSON data at http://jsonlint.com/ it was showing an unexpected character" error on the first line, attempting to point to the hidden characters but effectively pointing to the opening bracket of the JSON array which was totally throwing me off track. If I moved the cursor to the very beginning of the entry field at http://jsonlint.com/ and move one character to the right, the cursor wouldn't move. When I pressed the Right arrow a second time it did!  Bad sign. If I then back-spaced, it deleted the hidden character and the JSON validated as ok (this after pulling my hair out over countless attempts and tests). There was indeed something getting output by the php files but I still couldn't see what it was.

Visual studio has an option to show white space characters (vs 2008 it's in the  Edit - Advanced - View White Space menu) but nothing was showing up before the opening php tag. Normally I'd be looking for an errant echo or print which would cause this kind of thing but I'd removed all php code from the file and was still getting the error.

I finally opened the file in a little file tool I threw together while working with wave files that lets you look at the raw contents of any file and found the values "EF BB BF" at the beginning of the file.  Turns out, these extra characters are the aforementioned signature bytes which, ironically, were added to help make the file easier to use.  The EF BB BF values make up a signature value for "Unicode (UTF-8 with signature) " encoding.  The jQuery JSON parser ignores non-ASCII characters at the beginning of the PHP data but not any secondary characters which in my case were being contributed by these signature-sweetened require_once files. (explicative removed:)

The fix is really easy on vs2008: open each file, selected File - Advanced Save Options and set the Encoding combo box to "Unicode (UTF-8 without signature) - Codepage 65001" (I don't know what the Codepage business is about). When I saved the files and post them back to the server I stopped getting the error. 

The reason the require_once wasn't causing the problems with all the files is because I hadn't yet opened/saved them in Visual Studio yet.  I burned an embarrassing number of hours on this one so I hope this post saves someone some time.