PHP String Type explanation

Source: Internet
Author: User

PHP supports eight primitive types (type).

Four types of scalar:

    1. String (String)
    2. Integer (integer type)
    3. Float (floating point type, also double)
    4. Boolean (Boolean)

Two kinds of composite types:

    1. Array (arrays)
    2. Object (Objects)

Two special types:

    1. Resource (resources)
    2. Null (empty)

String Type

A string is a sequence of characters, just like "Hello world!".

There are 3 ways to define a string:

    1. Single quotation mark (')
    2. Double quotation marks (")
    3. Delimiter (<<<)
1. Single quotation mark definition

Enclosing a string in single quotation marks (') is the simplest way to define a string:

?
123 <?php$var_char= ‘码农教程‘;?>

If there is a single quotation mark in the string, you need to escape using a backslash (\):

?
1234 <?php$var_char= ‘字符串中包含\‘需要转义‘;echo$var_char;     //输出:字符串中包含‘需要转义?>

Tip: If a backslash is required before the single quotation mark or at the end of the string, it needs to be represented by two backslashes. In other words, the backslash escapes only the ' character ' and does not escape other characters.

If you simply define a simple string without parsing more content, it is obviously more efficient to use single quotes.

2. Double quotation mark definition

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

Escape Sequences Description
\ n Line break
\ r Enter
\ t Horizontal tab
\[/td> Reverse Slash
\$ Dollar sign
\" Double quotes
\[0-7]{1,3} This regular expression sequence matches a character represented by an octal symbol
\x[0-9a-fa-f]{1,2} This regular expression sequence matches a character that is represented by a hexadecimal symbol

Example:

?
123 <?php$var_char= "这里有个换行\n这是第二行文字";?>

Note that the line break in this example refers to the text meaning of line breaks, if you need to display a newline in the browser, you need to use the NL2BR () function to replace \ n to <br/>.

The most important point of a double-quote string is that the variable name is replaced by the value resolution of the variable:

?
1234 <?php$var_char= "这是一些文字";echo"请打印这些文字:$var_char";   //输出:请打印这些文字:这是一些文字?>
3. Delimiter definition

The delimiter definition string is the delimiter syntax (<<<), and an identifier is provided after the delimiter, followed by the defined string, and finally the same identifier ending string definition.

Example:

?
1234567 <?php$str= <<<EOD我是用定界符定义字符串的例子这是其他更多字符……EOD;?>

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

Note: the line where the end identifier is located cannot have any other characters (possibly except a semicolon; , including line indentation, and so on.

Hint: The string is not just a number of literal characters shown in the example above, but it can also be defined as a string, or even a large long text, which is not the same as the char definition in the database.

PHP String Type explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.