<?php
// begin the session
session_start();

// echo the session valuable
echo 'The value of sess is '.$_SESSION['sess'];
?>

I am new to php.
I am using php version 5.2 and Windows7.
I am not sure which encoding I should use to save a php file.
I am building a website with Chinese characters.
I have the above simple code to start with.

  1. If I save it as "UTF-8" encoding I get error

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\websites\page1.php:1) in C:\websites\page1.php on line 3
Setting value of sess

  1. If I save it as "unicode" encoding I don't get the echo text on screen

  2. If I save it as "ANSI" encoding I can't insert Chinese characters.

What should I do??

    Save it as UTF-8 without BOM. How you do this (or even if you can) will vary depending upon your editor of choice.

      More info on BOM. The basic idea is that your text editor put this byte sequence as the first data in your text file and your php script is outputting it to stdout. Once you have output any thing, you are not permitted to send headers.

      It's my understanding that the PHP language itself is a subset of ASCII text -- meaning all of the chars you need to express a PHP script are proper ASCII chars. That being the case, you might consider saving your PHP scripts as ANSI or ISO-8859-1 and putting all of your textual data in some other storage medium -- external files, a database, etc.

      It's also important to realize that PHP's basic string function assume one byte per char and do not properly support UTF-8 or other multibyte character sets. In order to deal with those properly, you'll need to use the multibyte string functions.

        UTF-8 should work fine, though, as long as you leave out the byte order mark (which is optional anyway with UTF-8 and probably not needed by anything out there).

          If you're using windows, it looks like Notepad 2 supports various utf-8 and utf-16 encodings, but doesn't really describe what they are.

            Write a Reply...