Search code examples
phpnetbeanscharacter-encodingwhitespacefile-encodings

Spaces in PHP causing meaningless errors?


I'm editing the functions.php file of a WordPress theme and whenever I edit it, even when the edit is adding a single space the server returns

error:

Parse error: syntax error, unexpected '}' in /theme/functions.php on line 1     

This is where I add the single space:

<?php load_theme_textdomain('theme', get_template_directory() . '/languages'); 
 <— I added a single space here

and everything dies after that. This is not a consistent error, as it goes away after I replace the whole functions.php file itself with a backed up one. It only is replicated when I edit the file, that is, as presented even with single white-space character. I'm using NetBeans and CuteFTP to edit then upload the files.

What is going on?! This must have to do with some kind of file quality itself, since from a code standpoint the code is exactly the same...or maybe my server.

Solution:
Netbeans defaults to a specific file encoding per project. Foreign files may have a different encoding which may cause problems. Cody the foreign file's contents to a new blank file which you created via Netbeans.


Solution

  • It could be an encoding error. Maybe when you try to edit the file your text editor is including a UTF-8 BOM at the top.

    EDIT

    Just found instructions for saving as UTF-8 without BOM in Notepad++:

    1) Click on "Format" Select "Encode in UTF-8 without BOM"

    2) Click on "settings" Click on "Preferences" Click on "new document/open save directory" Select "UTF-8 without BOM"

    (Source: http://www.phpbb.com/community/viewtopic.php?f=66&t=1584655#p9490925, 5th answer from the bottom)

    But the encoding you need to set really depends on the original file. If it's not UTF-8, you should keep the original encoding (Latin 1 or whatever).