String Data Type, Literals and Functions

This section providing information on string data type, literals, and functions.

PHP supports string data type as a sequence of 8-bit characters. Base on this definition, you can handle both text data and binary data as strings. This is very different than Java, where you need to String to handle text data and byte[] to handle binary data.

In PHP, string variables are normal scalar variables with no explicit type declarations. String literals are written in 3 forms: single quoted, double quoted, and heredoc. Here is a short PHP script with some examples of string literals and variables:

<?php 
#- String-Literals.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.

# Single quoted string with escape characters
  $text = 'Thank God it\'s Friday!';

# Double quoted string with variables included
  $beer = 'Heineken';
  $message = "$beer's taste is great.";

# Heredoc string
  $paragraph = <<<END_OF_STRING
In PHP, string variables are normal scalar variables 
with no explicit type declarations. String literals 
are written in 3 forms: single quoted, double quoted, 
and heredoc. ...
END_OF_STRING;

  print("\n$text\n");
  print("\n$message\n");
  print("\n$paragraph\n");
?>

If you run it with PHP CLI, you will get:

C:\>\local\php\php String_Literals.php

Thank God it's Friday!

Heineken's taste is great.

In PHP, string variables are normal scalar variables
with no explicit type declarations. String literals
are written in 3 forms: single quoted, double quoted,
and heredoc. ...

Since PHP strings are sequences of 8-bit characters, we can use them as binary strings to store Chinese character strings in UTF-8, GB18030, or Big5 encoding. But I strongly suggest you to use UTF-8 encoding for your Chinese strings in PHP. It can handle both simplified characters and traditional characters.

Last update: 2015.

Table of Contents

 About This Book

 PHP Installation on Windows Systems

 Integrating PHP with Apache Web Server

 charset="*" - Encodings on Chinese Web Pages

Chinese Characters in PHP String Literals

String Data Type, Literals and Functions

 String Literal Travel Path

 Chinese Character String with UTF-8 Encoding

 Chinese Character String with GB18030 Encoding

 Chinese Character String with Big5 Encoding

 UTF-8 Encoding Pages with Big5 Characters

 Multibyte String Functions in UTF-8 Encoding

 Input Text Data from Web Forms

 Input Chinese Text Data from Web Forms

 MySQL - Installation on Windows

 MySQL - Connecting PHP to Database

 MySQL - Character Set and Encoding

 MySQL - Sending Non-ASCII Text to MySQL

 Retrieving Chinese Text from Database to Web Pages

 Input Chinese Text Data to MySQL Database

 References

 PDF Printing Version