Monday, February 7, 2011

Strange characters in PHP

This is driving me crazy.

I have this one php file on a test server at work which does not work.. I kept deleting stuff from it till it became

<?
print 'Hello';
?>

it outputs

Hello

if I create a new file and copy / paste the same script to it it works! Why does this one file give me the strange characters all the time?

  • That's the BOM (Byte Order Mark) you are seeing.

    In your editor, there should be a way to force saving without BOM which will remove the problem.

    From Pat
  • Is the document saved in an encoding such as UTF-8/16? What encoding does the html-page specify?

    From Patrik
  • Wow that was fast guys !

    Thanks I will check it out.

  • We had the same thing happen once upon a time it drove the dev crazy for a while too :-)

    Someone had opened the file and accidentally saved it with BOM and from there on the editor would preserve the BOM while saving the file.

    In your editor, there should be a way to force saving without BOM which will remove the problem.

    From Pat
  • Found it, file -> encoding -> UTF8 with BOM , changed to to UTF :-)

    I should ahve asked before wasing time trying to figure it out :-)

  • Just in case, here is a list of bytes for BOM

    Encoding    Representation (hexadecimal)
    UTF-8   EF BB BF
    UTF-16 (BE) FE FF
    UTF-16 (LE) FF FE
    UTF-32 (BE) 00 00 FE FF
    UTF-32 (LE) FF FE 00 00
    UTF-7   2B 2F 76, and one of the following bytes: [ 38 | 39 | 2B | 2F ]†
    UTF-1   F7 64 4C
    UTF-EBCDIC  DD 73 66 73
    SCSU    0E FE FF
    BOCU-1  FB EE 28 optionally followed by FF†
    

0 comments:

Post a Comment