There are two ways to replace variables with strings--simple methods and complex methods.
The simple approach is to put the variable name in a double-quote string or Heredoc:
$who = ' Kilroy ';
$where = ' here ';
echo "$who was $where";
Kilroy is here
The complex approach is to enclose the variable to be replaced in curly braces. This method can be used to disambiguate or replace array lookups. The classic function of curly braces is to separate the variable names from the surrounding text:
$n = 12;
echo "You is the {$n}th person";
You is the 12th person
Without curly braces, PHP will try to print out the value of the variable $nth.
Unlike some shell environments, variables are not parsed in PHP strings, but only in double-quoted strings, and the result is treated as a string value:
$bar = ' This was not printed ';
$foo = ' $bar '; Single quotation marks
Print ("$foo"); Double quotes
$bar
4.1.2 strings enclosed in single quotes
Single-quoted Strings
Strings enclosed in single quotation marks do not replace variables. Because the string literals are enclosed in single quotes, the variable names are not parsed in the following string:
$name = ' Fred ';
$str = ' Hello, $name '; single-quoted enclosed in single quotation marks.
Echo $str;
Hello, $name
The only escape sequence available in a string enclosed in single quotation marks is \ ' (enclose the single quotation mark in a string enclosed in single quotation marks), \ \ (place a backslash in a string enclosed in single quotation marks). Any other backslashes can only be interpreted as a backslash:
$name = ' Tim o\ ' Reilly '; Escaped single quotation mark
Echo $name;
$path = ' c:\\windows '; Escaped backslash
echo $path;
$nope = ' \ n '; is not an escape sequence
Echo $nope;
Tim O ' Reilly
C:\WINDOWS
\ n
4.1.3 strings enclosed in double quotes
Double-quoted Strings
Strings enclosed in double quotes will parse the variables and allow many escape sequences to be used. Table 4-1 lists the escape sequences that PHP endorses in the string enclosed in double quotation marks.
Table 4-1: Escape sequences in a string enclosed in double quotation marks
Escape sequence character meaning
\”
Double quotes
\ n
Line break
\ r
Enter
\ t
Tabs
\\
Back slash
\$
Dollar sign
\{
Left Curly Brace
\}
Closing curly Braces
\[
Left Middle Bracket
\]
Right Middle Bracket
Through \777
ASCII characters expressed in octal
\x0 through \xff
ASCII characters in hexadecimal notation
If an unknown escape sequence is found in a string enclosed in double quotation marks (for example, a backslash followed by a character not in table 4-1), the escape sequence is ignored (if the warning level is set to E_notice, a warning is generated for such an unknown sequence):
$str = "What are \c this?"; Unknown escape sequence
Echo $str;
What's \c this?
4.1.4 String Delimitation
Here Documents Heredoc
Using Heredoc, you can simply put a multiline string in the program, as follows:
$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 terminating identifier to end the statement, as shown in the previous code. If you use Heredoc in a more complex expression, you need to write the expression branch:
printf (<<< Template
%s is%d years old.
Template
, "Fred", 35);
Single and double quotes 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?"
Whitespace characters in Heredoc are also preserved:
$ws = <<< Enough
Boo
Hoo
enough;
$ws = "boo\n hoo\n";
Because the newline character before the end Terminator is removed, the following two assignments are the same:
$s = ' Foo ';
Same as and below are the same
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
If you want to end a string of heredoc references with a newline character, you'll need to add your own extra:
$s = <<< End
Foo
End;
Note that Foo is followed by a blank line and cannot be deleted.
The above describes the variable definition of PHP variable definition and variable substitution methods, including the definition of variables, I hope to be interested in PHP tutorial friends helpful.