Welcome to 92798.NET [Post Free/Paid hosting information] [Contact US]
HOME Documentation Free Hosting Paid Hosting Tools Search
Current : HOME >> Documentation >> Web >> PHP >> Content

PHP's string representation, you know?

From : Unknown, View : 387, 2009-10-09 18:33:00
PHP-that the string is the first hurdle. No one program can leave the string. But you know the string representation of the number of ways, how do magic? If you have carefully read this article, will give your PHP journey to help.

String literal approach can be three kinds of definitions,
Single quote
Double quotes
Delimiter (HERE documents)

Single quote
To specify a simple string The easiest way is to use single quotes ( ') marks. For example:

The following is a PHP code: 【copy the code】
 
echo 'We are letters!';
echo 'can also have the Chinese';
echo 'wrap can also be directly included
This is the second line of the
The third line
...';
  Of course, you need to click your browser's "View source" in order to see the effect of wrap. "

Single quotes should not appear single quotation marks. Should be in single quotes to represent a single quote, need to use a backslash (\) escape, and many other languages. If the end of the string before a single quote or a backslash need arose a need to use two backslashes said. Note that if you try to escape any other character (that is, apart \ and '), the backslash will also be displayed! So, usually do not need to escape the backslash itself.

The following is a PHP code: 【copy the code】
 
echo 'Here is a single quote \', He He ';
echo 'two backslash represents a backslash \ \, Hei hei';
echo 'If the backslash is not a single quote or backslash behind, the backslash will directly show up \, \ n, \ t en ...';
  An ambush, and the other two kinds of grammar is different from the string appear in single quotes variables and escape sequences will not be the value of the variable substitution.

Double quotes
If you use double quotes ( ") is enclosed in the string, PHP has more powerful functions.
Double quotes to support more escape sequences:
Sequence of the meaning of
\ n newline (LF or ASCII character 0 × 0A (10))
\ r carriage return (CR or ASCII characters 0 × 0D (13))
\ t horizontal tab (HT or the ASCII character 0 × 09 (9))
\ \ Backslash
\ $ Dollar sign
\ "Double quotes
\ [0-7] (1,3) This regular expression matches a sequence of octal notation with the character
\ x [0-9A-Fa-f] (1,2) This regular expression matches a sequence of hexadecimal notation with the character

In addition, and single quotation marks, as if trying to escape any other character, the backslash will also be displayed! Prior to PHP 5.1.1 in, \ ($ var) in the backslash will not be displayed. [Level nets: http://goalercn.com]

Double quotation marks should be double-quotes must be \ "[standard network: http://goalercn.com]

Double quotation marks is one of the more powerful variable variable names can be replaced! [Level nets: http://goalercn.com]

If you encounter a dollar sign ($), the parser will be achieved as much as possible behind the characters to form a valid variable name. If you want to explicitly specify the end of the name, use curly braces to enclose the variable name.

The following is a PHP code: 【copy the code】
 
$ beer = 'Heineken';
echo "$ beer's taste is great"; / / work,? $ beer has been replaced by Heineken, double quotes inside single quotes may appear
echo "He drank some $ beers"; / / work, but there is no $ beers of this variable, so $ beers have been replaced with an empty
echo "He drank some $ (beer) s"; / / Work
echo "He drank some ($ beer) s"; / / Work
echo "<a href=\"http://www.bnet.cm.cn\" target=\"_blank\" title=\"商业英才网\"> BNET </ a>"; / / There are many pairs of quotation marks this time is more convenient to use delimiter
  The same can also parse array index or an object property. For the array index, right bracket (]) marks the end of the index.

The following is a PHP code: 【copy the code】
 
error_reporting (E_ALL);

$ fruits = array ( 'strawberry' => 'red', 'banana' => 'yellow');
/ / Work, but the banana is the first as a constant, when there is no definition of this constant is a string that look, so there is hidden danger in this way
echo "A banana is $ fruits [banana]";
/ / Work, which is the standard array of writing, it is recommended
echo "A banana is ($ fruits [ 'banana']}.";

/ / Does not work, which must be enclosed with ()
echo "A banana is $ fruits [ 'banana'].";

/ / Work

echo "This square is $ square-> width meters broad.";

/ / Variable substitution can be a lot of grammatical simplification, for example:

echo "My name is". $ name. ", you?"; delimiter (HERE documents)
Another way to delimit the string delimiter syntax to use ("<<<")。 Should be In "" "provided after an identifier, then the string, and then the same identifier to close the string.

The closing identifier must begin from the first column of the line. Similarly, the identifier must follow any other label in PHP naming rules: must contain only alphanumeric characters and underscores, and must begin with an underscore or a non-numeric characters.

For example:

The following is a PHP code: 【copy the code】
 
$ outstr = <<<EOF
Here you can have single quotes and double quotes and with the escape sequence in double quotes
'
"
\ n
\ \
EOF;
  Delimiter excellent point is that with the double quotes can contain double quotes and single quotation marks.

A very important point must be noted that the end of the line where the identifier can not contain any other characters, except possibly a semicolon (;) outside. That means especially that the identifier can not be indented, and both before and after the semicolon can not have any spaces or tabs. Equally important is to realize that before the end of the first character of an identifier must be defined in your operating system line breaks. For example, in Macintosh systems is \ r.

If the destruction of this rule makes the closing identifier is not "clean", then it will not be regarded as the end of identifiers, PHP will continue looking. If you can not find in this case the end of the identifier will lead to a last line of the script syntax error occurs.

Delimiter syntax can not be used to initialize class members. Use other string syntaxes instead.

Delimiter text behaves like a double-quoted string, but no double-quotes. This means do not need to escape quotes in the text delimiter, but you can still use the escape codes listed above. Variable will be expanded, but the delimiter text strings when expressing complex variables and the same care must be.
TAGS:PHP
  • Say Something
  • Comment List
COMMENT
Your Name :      Contact : (OPTIONAL)
NOTICE:
1, No Ads, No Spam;
2, Prohibition of personal attacks;
3, Less then 2000 characters;
Security Code: Refresh
No Comment