Jump to content

php's default character set


digitalecartoons

Recommended Posts

PHP doesn't have a default character set.  It defaults to representing strings as 8 bit characters (256 possible values if you include null), which could represent many different character sets.  You can even store utf8 strings as long as you remember that functions like strlen() won't count characters, they will count bytes.

Link to comment
Share on other sites

Ok, so the quote "PHP assumes that strings are ISO-8859-1" like someone told me isn't completely true? Can you explain this: when I have a php file which only echoes the é character, the browser defaults to iso-8859-1. When I change the browser charset to unicode it displays a question mark instead, but when I refresh the page or type in the php link again, it switches back to iso 8859 again. Even though I haven't specifically set it as such.

Link to comment
Share on other sites

Well there's a semantic issue here .. strings in php don't have any character set.  But the final document produced by php has a character set.  When the browser makes a request, the server (where php is running) will usually specify which character set to interpret the document in.  And that's affected by what effigy mentioned.

 

So what I would say is "PHP does not assume that strings are ISO-8859-1", BUT php does assume that its output is iso-8859-1, and will tell the browser so.  That's nothing to do with strings internally in php, which can be in any character set.  They are simply data.

 

To show what I mean, consider the following code:

 

$iso8859 = 'é';
$utf8 = iconv('ISO-8859-1', 'UTF-8', $iso8859);

 

The first string is iso8859-1, but the second is utf8.  Both are in the same script.  And php neither knows nor cares what encoding each is in.  That's up to you to take care of.

 

But when you produce the final HTML document, the browser needs to know the encoding.  That gets sent with the response headers (and can be changed manually by the user, as you described).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.