PHP Data type string

Define a String Type Example:

$var_char = " Hello! ";

In this example, we define a string: Hello, and assign this string to the variable $var_char, which is also automatically defined as a string.

There are three ways to define a string:

  1. Single quotes(')
  2. Double quotes(")
  3. Delimiters(<<<)

single quote definition

The easiest way to define a string is to enclose the string in single quotation marks('):

$var_char = 'This is some string';

If the string has single quotation marks, then you need to use a backslash(\) to escape:

$var_char = 'string contains' need to escape';
echo $var_char; // Output: string contains' need to escape

Tip: If you need a backslash before the single quote or at the end of the string, use two backslashes. In other words, the backslash only escapes the 'characters' without escaping other characters.

If you just define a simple string without having to parse more content, it is clearly more efficient to use single quotes.

Double quotation definition

If you define a string in double quotation marks("), PHP understands the escape sequences for more special characters:

escape sequence Description
\n Wrap
\r Enter
\t Horizontal tab
\/ Backslash
$ dollar sign
\" double quotation marks
\[0-7] {1,3} This regular expression sequence matches a character in octal notation
\x [0-9A-Fa-f] {1,2} This regular expression sequence matches a character in hexadecimal notation


$var_char = " There is a newline here \nThis is the second line of text ";

Run the example to view the source code of the page:

There is a newline here
This is the second line of text

Note that the newline in this example means line breaks in the textual sense. If you want to display the line breaks in the browser, replace n with the nl2br() function to replace <br>.

The most important point of thedouble-quoted string is that the variable name will be parsed by the variable value:

$var_char = " This is some text ";
echo " Please print these words: $var_char "; // Output: Please print these words: This is some text

delimiter definition

The delimiter character string is defined using the delimiter syntax(<<<<) and provides an identifier followed by a delimiter followed by a defined string followed by the same identifier ending with a string definition .


$ Str = <<<<< EOD
I used string delimiter definition examples
This is more of the other characters

In this example, EOD is the identifier. The end identifier must start at the first column of the row. Similarly, the identifier must follow any other tag naming convention in PHP: it can only contain alphanumeric underscores and must begin with an underscore or a non-numeric character.

Note: The end of the line where the identifier can not have any other characters(possibly in addition to a semicolon; other), including the line indentation.


String not only refers to the above example shows a number of characters, can also be defined as a string of characters, or even a huge long text, which is the database and the char definition is not the same place.

Copyright 2014-2017 by All Rights Reserved.