0

The program I am writing has to output utf-8. When the page is loading the php looks for the $_GET['id'] and echo's something for each keyword.Example:

if($_GET['id']=="keyword1"){
echo('reply for keyword1');
}else if($_GET['id']=="keyword2"){
echo('reply for keyword2');
}

Those reply's from the "echo" command have to be in UTF-8, because the page is only accessed by another program who can only understand UTF-8.

When I try to read the page the output is not exactly UTF-8.

Question 1: What type of encoding does the standard echo command? Question 2: If echo does not output UTF-8 but something else. How can I convert the standard output of the echo command in UTF-8?

  • 1
    possible duplicate of [How to best configure PHP to handle a UTF-8 website](http://stackoverflow.com/questions/1605760/how-to-best-configure-php-to-handle-a-utf-8-website) – MrTux Aug 17 '14 at 12:13
  • `echo` outputs whatever you tell it to output; `echo` doesn't know or care about charsets.... it's up to your code to ensure that the values you're outputting using echo are utf-8 – Mark Baker Aug 17 '14 at 12:23
  • how can I tell echo to output utf-8? –  Aug 17 '14 at 12:24
  • Set the file encoding to UTF-8 without BOM and send a header to the browser telling the Content-Type with charset. – Charlotte Dunois Aug 17 '14 at 12:26
  • Can you please give me some sample code? –  Aug 17 '14 at 12:31

1 Answers1

1

echo outputs whatever data you give it. In PHP, strings are simple byte arrays and do not have an associated encoding that PHP is aware of. If your byte array string contains UTF-8 encoded text, echo will output UTF-8 encoded text. If it contains the raw bytes for a JPEG image, echo will output a JPEG image.

How your string is encoded depends on where you get it from. If it's a string literal in the source code, it'll have the encoding of the file it's in. If you save the file as UTF-8 in your text editor and write echo 'Foo'; in it, echo will output "Foo" encoded in UTF-8.

See UTF-8 all the way through and What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889