There are two ways to replace variables into strings - the simple way and the complex way.
The easy way is to put the variable name in a double quoted string or a heredoc:
$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here
Complex The method is to enclose the variable to be replaced in curly brackets. This approach can be used to disambiguate or replace array lookups. The classic function of braces is to separate the variable name from the surrounding text:
$n = 12;
echo “You are the {$n}th person”;
You are the 12th person
If there are no braces, PHP will try to print out the value of the variable $nth.
Unlike some shell environments, variables in PHP strings will not be parsed repeatedly, but will only be parsed in double-quoted strings, and the result will be used as the value of the string:
$bar = 'this is not printed ';
$foo = '$bar'; // Single quotes
print("$foo"); // Double quotes
$bar
4.1.2 Strings enclosed in single quotes
Single-Quoted Strings
Use Strings enclosed in single quotes do not replace variables. Because string literals are enclosed in single quotes, variable names are not parsed in the following string:
$name = 'Fred';
$str = 'Hello, $name'; // single-quoted Enclosed in single quotes
echo $str;
Hello, $name
The only escape sequence available in a string enclosed in single quotes is ' (putting single quotes inside a string enclosed in single quotes) , \ (putting a backslash inside a string enclosed in single quotes). Any other backslash will only be interpreted as a backslash:
$name = 'Tim O'Reilly'; //Escaped single quote
echo $name;
$path = 'C:\WINDOWS'; //Escaped backslash
echo $path;
$nope = 'n'; // Not an escape sequence
echo $nope;
Tim O'Reilly
C:WINDOWS
n
4.1.3 Use double quotes Surrounded Strings
Double-Quoted Strings
Strings enclosed in double quotes will be variable parsed and many escape sequences are allowed. Table 4-1 lists the escape sequences that PHP recognizes in strings enclosed in double quotes.
Table 4-1: Escape sequences in strings enclosed in double quotes
Escape sequence character meaning
”
Double quotes
n
Line feed
r
Carriage return
t
Tab character
\
Backslash Bar
$
Dollar sign
{
Left brace
}
Right brace
[
Left bracket
]
Right bracket
If you use a heredoc in a more complex expression, you need to write the expression on separate lines:
printf(<<< Template
%s is %d years old.
Template
, “Fred”, 35 );
Single and double quotes in the heredoc are skipped (treated as normal symbols):
$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 in the heredoc is also preserved :
$ws = <<< Enough
boo
hoo
Enough;
// $ws = ” boon hoon”;
Because the newline character before the end terminator will be removed, the following two The assignment is the same:
$s = 'Foo';
// same as
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
If you want to end the heredoc reference with a newline character String, you need to add it yourself:
$s = <<< End
Foo
End;
//Note that Foo is followed by a blank line and cannot be deleted
The above introduces the methods of variable definition, PHP variable definition and variable substitution, including the content of variable definition. I hope it will be helpful to friends who are interested in PHP tutorials.