PHP Basics String (1) _php tutorial

Source: Internet
Author: User
Tags escape quotes php basics
string

String is a series of characters. In PHP, the characters and bytes, that is, a total of 256 different characters of the possibility. This also implies that PHP does not have local support for Unicode. See Functions Utf8_encode () and Utf8_decode () For more information about Unicode support.

Note: A string becomes very large and has no problem, PHP does not impose a range of implementations on the size of the string, so there is no reason to worry about long strings at all.

Grammar

The string can be defined in three literal ways.

Single quotation marks

Double quotes

Delimiter

Single quotation marks

The simplest way to specify a simple string is to enclose it in single quotation marks (characters ').

To represent a single quotation mark, you need to escape with a backslash (), as in many other languages. 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. Note that if you try to escape any other characters, the backslash itself will be displayed as well! Therefore, it is usually not necessary to escape the backslash itself.

Note: in PHP 3, this situation will issue a e_notice level warning.

Note: Unlike the other two syntaxes, the variables and escape sequences that appear in the single-quote string are not substituted by the value of the variable.

     

Double quotes

If you enclose strings in double quotation marks ("), PHP knows more about escape sequences for special characters:

Table 11-1. Escape character

Sequence meaning

N line break (LF or ASCII characters 0x0A (10))

R Enter (CR or ASCII character 0x0D (13))

T horizontal tab (HT or ASCII character 0x09 (9))

\ Backslash

$ dollar Sign

"Double quotation marks

[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

Also, if you try to escape any other characters, the backslash itself will be displayed!

The most important point of a double-quote string is that the variable name is substituted by the value of the variable. See string parsing for details.

Delimiter

Another way to set the bounds of a string is to use the delimiter syntax ("<<<"). You should provide an identifier after <<<, followed by a string, followed by the same identifier end string.

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.

Warning

It is important to note that the line where the end identifier is located cannot contain any other characters, except for a semicolon (;). This in particular means that the identifier cannot be indented and cannot have any spaces or tabs before and after the semicolon. It is also important to realize that the first character before the end identifier must be a line break defined in your operating system. For example, in the Macintosh system is R.

If you break this rule so that the end identifier is not "clean", it will not be considered a closing identifier, and PHP continues to look for it. If the appropriate end identifier is not found in this case, it will result in a syntax error that appears in the last line of the script.

The delimiter text behaves just like a double-quoted string, with no double quotes. This means that you do not need to escape quotes in the delimiter text, but you can still use the escaped code listed above. Variables are expanded, but when you express complex variables in the delimiter text, you should be aware of the same as the string.

Example 11-2. Delimiter String Example:

     
     foo = ' foo ';       $this->bar = Array (' Bar1 ', ' Bar2 ', ' Bar3 ');}   } $foo = new Foo (); $name = ' MyName '; echo <<
     
     
      
      foo. Now, I am printing some {$foo->bar[1]}. This should print a capital ' a ': x41eot;? >
     
     

Note: The delimiter support is added in PHP 4.

1

http://www.bkjia.com/PHPjc/446737.html www.bkjia.com true http://www.bkjia.com/PHPjc/446737.html techarticle strings string is a series of characters. In PHP, the characters and bytes, that is, a total of 256 different characters of the possibility. This also implies that PHP has no local support for Unicode ...

  • 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.