Chinese Characters in String Literals - UTF-8 Encoding

This section provides a tutorial example on how enter and use Chinese characters PHP string literals using Unicode UTF-8 encoding. The PHP script file should be stored in UTF-8 format.

Now let's play with Chinese characters. They are definitely harder to work with than French characters. My first example shows you how to handle Chinese characters in PHP scripts with UTF-8 encoding.

1. On a Windows system, run Start > All Programs > Accessories > Notepad.

2. In Notepad, enter the following HTML document:

<?php #HelpUtf8French.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
#
   print('<html>');
   print('<meta http-equiv="Content-Type"'.
      ' content="text/html; charset=utf-8"/>');
   print('<body>');
   print('<b>说明</b><br/>');
   print('这是一份非常间单的说明书…<br/>');
   print('</body>');
   print('</html>');
?>

3. As I mentioned earlier in this book, entering Chinese characters is not an easy job. You need to use a Chinese Windows system, or a Chinese input tool on a non-Chinese Windows system. If you don't have any Chinese input tool, you can simply go to the Yahoo Chinese Web site, http://www.yahoo.com.cn/, copy some Chinese characters, and paste them into Notepad. The Yahoo Chinese Web site is encoded in UTF-8.

4. Select menu File > Save as. Enter the file name as HelpUtf8Chinese.php. Select "UTF-8" in the Encoding field and click the Save button.

5. Copy HelpUtf8Chinese.php to c:\inetpub\wwwroot. Make sure your Internet Information Service is running the local default Web site.

6. Now run Internet Explorer (IE) with http://localhost/HelpUtf8Chinese.php. Your should see the Chinese characters displayed correctly.

7. On the IE window, select menu View > Encoding. You should see UTF-8 is selected.

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

Using Non-ASCII Characters as PHP Script String Literals

 Basic Rules of Using Non-ASCII Characters in HTML Documents

 French Characters in String Literals - UTF-8 Encoding

 French Characters in HTML Documents - ISO-8859-1 Encoding

Chinese Characters in String Literals - UTF-8 Encoding

 Chinese Characters in String Literals - GB2312 Encoding

 Characters of Multiple Languages in String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version