PHP: Strings (string) data type instance detailed

Source: Internet
Author: User
what is a string?

A string is a sequential sequence of characters, consisting of numbers, letters, and symbols. Each character in the string occupies only one byte.

In PHP, there are three ways to define strings, namely single quotation marks ('), double quotation marks ("), and delimiters (<<<).

Single and double quotes are often used to define a string, defined as follows:

Single quotation marks

<?php$a= ' topic.alibabacloud.com ';? >

Double quotes

<?php$a= "topic.alibabacloud.com";? >

The difference between single and double quotes is that the variables contained in the double quotes are automatically replaced with the actual values, and the variables contained in the single quotation mark are created as normal characters, so what do you mean? Let's take a look at the example below.

Examples of single and double quotation marks where strings differ

<?phpheader ("Content-type:text/html;charset=utf-8");              Set the encoding $i= ' topic.alibabacloud.com ';                                              Declares a string variable echo "$i";                                                   Output echo "<p>" with double quotes;                                                  Output short mark echo ' $i ';                                                   Output?> with single quotes

Example run results such as:

An example is detailed:

In this example, we use single and double quotes to output the same variable, but the result of the output is completely different, as we can see, the result of our use of double quotation marks is the value of the variable, and the single quote output is the string "$i".

In addition to the above differences, there is no difference in the use of the transfer characters. Single quotation marks are used as long as the single quotation mark "'" is transferred, but when using double quotation marks ("), also pay attention to the use of characters such as" "", "$", these special characters are to be displayed by the escape character "\", the commonly used escape characters are as follows:

Commonly used escape characters

Escape character Output
\ n Line break
\ r Enter
\ t Horizontal tab
\\ Back slash
\$ Dollar sign
\' Single quotation marks
\" Double quotes
\[0-7]{1,3} The regular expression matches the character represented by an octal symbol
\x[0-9a-fa-f]{1,2} The regular expression matches the character represented by a hexadecimal symbol

One thing to note here is that \ n and \ r are no different in Windows systems, and can be used as carriage return characters. However, in the Linux system is two effects, in Linux, \ n for the next line, but not back to the beginning, and \ R for the cursor back to the beginning, but still in the bank, if you have a Linux operating system, you can try it.

If we use "\" for non-escaped characters, then "\" will be output with the output.

In defining some simple strings, using single quotes is a better way to handle it. If you use double quotation marks, PHP will take some time to deal with the transfer of strings and the parsing of variables, so if there is no special requirement when defining a string, we'd better use single quotes

delimiting characters

The delimiter (<<<) is supported from PHP4.0. After using an identifier followed by a string, and finally the same identifier end string. The format of the delimiter is as follows:

$string =<<< str to output the string str

Where STR is the specified identifier

delimiter Example

The code is as follows:

<?phpheader ("Content-type:text/html;charset=utf-8");              Set the encoding $i= ' topic.alibabacloud.com ';                                              Declare a string variable echo <<<std This is no different from double quotes, and the \ $i can be output as well. The contents of the <p>\ $i are: $istd?>

Code Run Result:

Detailed Examples:

In the above example, we use the value of the delimiter output variable, and we can see that it is no different from the double quotation mark, and the included variable is also replaced with the actual value.

The end identifier must be a separate row, and no spaces are allowed. A system error, including comments, occurs when there are other characters or symbols before or after the identifier.

In the next section, we'll show you the Integer (integer) in the scalar data type.

Related Article

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.