PHP variable definition and replacement method _ PHP Tutorial

Source: Internet
Author: User
Tags sodium
PHP variable definition and variable replacement methods. There are two ways to replace variables with strings-simple methods and complex methods. The simple method is to put the variable name in a double quotation mark string or heredoc: $ who 'Kilroy; $ wh there are two ways to replace the variable with the string-simple methods and complex methods.
A simple method is to place the variable name in a double quotation mark string or heredoc:
$ Who = 'Kilroy ';
$ Where = 'where ';
Echo "$ who was $ where ";
Kilroy was here
The complicated method is to enclose the variable to be replaced with braces. This method can be used to eliminate ambiguity or replace array searches. The typical function of braces is to separate variable names from the surrounding text:
$ N = 12;
Echo "You are the {$ n} th person ";
You are the 12th person
Without braces, PHP will try to print the value of the variable $ nth.
Unlike some shell environments, variables in PHP strings are not parsed repeatedly, but are only parsed in double quotation marks, and the result is taken as the string value:
$ Bar = 'This is not printed ';
$ Foo = '$ bar'; // single quotes
Print ("$ foo"); // double quotation marks
$ Bar
4.1.2 string enclosed in single quotes
Single-Quoted Strings
Strings enclosed in single quotes do not replace variables. Because the number of strings is enclosed in single quotes, the variable names in the following strings are not parsed:
$ Name = 'Fred ';
$ Str = 'Hello, $ name'; // enclose single-quoted in single quotes
Echo $ str;
Hello, $ name
The only available escape sequence in a string enclosed by single quotes is \ '(put single quotes in a string enclosed by single quotes), \ (Put a backslash in a string enclosed by single quotes ). Any other backslash can only be interpreted as a backslash:
$ Name = 'Tim O \ 'Reilly '; // Escape single quotes
Echo $ name;
$ Path = 'C: \ Windows'; // escape backslash
Echo $ path;
$ Nope = '\ n'; // not an escape sequence
Echo $ nope;
Tim O 'Reilly
C: \ WINDOWS
\ N
4.1.3 string enclosed in double quotation marks
Double-Quoted Strings
Strings enclosed in double quotation marks are parsed as variables and many escape sequences are allowed. Table 4-1 lists escape sequences recognized by PHP in strings enclosed in double quotes.
Table 4-1: escape sequences in strings enclosed by double quotation marks
Escape sequence characters
\"
Double quotation marks
\ N
Line Feed
\ R
Enter
\ T
Tab
\\
Backslash
\ $
Dollar Sign
\{
Left braces
\}
Right braces
\[
Left brackets
\]
Brackets
\ 0 through/777
ASCII characters in octal format
\ X0 through \ xFF
ASCII characters in hexadecimal notation
If an unknown escape sequence is found in a string enclosed by double quotation marks (for example, a backslash is followed by a character not in Table 4-1 ), ignore this escape sequence (if the warning level is set to E_NOTICE, a warning will be generated for such unknown sequence ):
$ Str = "What is \ c this ?"; // Unknown escape sequence
Echo $ str;
What is \ c this?
4.1.4 string demarcation
Here Documents Ents heredoc
With heredoc, you can simply put multiple-line strings in the program, as shown below:
$ Clerihew = <End_Of_Quote
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
End_Of_Quote;
Echo $ clerihew;
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
< <符号(我们习惯称为字符串定界符――译者注)告诉php解析器你正在书写一个heredoc。在<<<符号和标识符(本例中即 end_of_quote)之间必须有一个空格,这样程序才可以辨别标识符。从下一行开始就是被引用的文本,直到它遇到仅由标识符组成的一行为止。
You can put the semicolon behind the end identifier to end the sentence, as shown in the previous code. If you use heredoc in a more complex expression, you need to branch the expression to write:
Printf (<Template
% S is % d years old.
Template
, "Fred", 35 );
The single and double quotation marks in heredoc are skipped (as a general symbol ):
$ Dialogue = <No_More
"It's not going to happen !" She fumed.
He raised an eyebrow. "Want to bet ?"
No_More;
Echo $ dialogue;
"It's not going to happen !" She fumed.
He raised an eyebrow. "Want to bet ?"
Blank characters in heredoc are also retained:
$ Ws = <Enough
Boo
Hoo
Enough;
// $ Ws = "boo \ n hoo \ n ";
Because the linefeed before the ending Terminator will be removed, the following two values are the same:
$ S = 'foo ';
// Same as is the same
$ S = <End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
If you want to end the string referenced by heredoc with a line break, you need to add the following additional characters:
$ S = <End
Foo

End;
// Note that Foo is followed by an empty line and cannot be deleted

Bytes. Simply put the variable name in a double quotation mark string or heredoc: $ who = 'Kilroy '; $ wh...

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.