PHP variable definitions and variable substitution methods _php tips

Source: Internet
Author: User
Tags sodium
There are two ways to replace a variable in a string--a simple method and a complex method.
The simple way is to put the variable name in a double quote string or Heredoc:
$who = ' Kilroy ';
$where = ' here ';
echo "$who was $where";
Kilroy was Here
The complex approach is to enclose the variable you want to replace with curly braces. This method can be used to disambiguate or replace array lookup. The classic function of curly braces is to separate the variable names from the surrounding text:
$n = 12;
echo "You are the {$n}th person";
are the 12th person
If you don't have curly braces, PHP will try to print out the value of the variable $nth.
Unlike some shell environments, variables do not parse in a PHP string, but only the parsing in a double quote string, and then the result is treated as a string value:
$bar = ' is not printed ';
$foo = ' $bar '; Single quotation mark
Print ("$foo"); Double quotes
$bar
4.1.2 strings enclosed in single quotes
Single-quoted Strings
Strings enclosed in single quotes do not replace variables. Because the string literal is enclosed in single quotes, the variable name is 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 that is available in single quotes is \ ' (Put single quotes in single quotes), \ \ (Put a backslash in a string enclosed in single quotes). Any other backslash can only be interpreted as a backslash:
$name = ' Tim o\ ' Reilly '; Escaped single quote
Echo $name;
$path = ' c:\\windows '; The escaped backslash
echo $path;
$nope = ' \ n '; 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 are parsed by variables and allow many escape sequences to be used. 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 Wrap
\ r
Enter
\ t
Tabs
\\
Back slash
\$
Dollar sign
\{
Left Curly Brace
\}
Right Curly brace
\[
Left Middle Bracket
\]
Right Middle Bracket
Through \777
ASCII characters represented by octal
\x0 through \xff
ASCII characters in hexadecimal
If an unknown escape sequence is found in a string enclosed in double quotes (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 is \c this?"; Unknown escape sequence
Echo $str;
What is \c this?
4.1.4 String Delimitation
Here Documents Heredoc
Using Heredoc, you can simply place multi-line strings in your program, as follows:
$clerihew = <<< End_of_quote
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
The having discovered sodium.
End_of_quote;
Echo $clerihew;
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
The having discovered sodium.
<<< symbols (which we used to call string delimiters--translators) tell the PHP parser that you are writing a heredoc. There must be a space between the <<< symbol and the identifier (End_of_quote in this case) so that the program can identify the identifier. Starting with the next line is the referenced text until it encounters a single line consisting of only identifiers.
You can end the statement by placing the semicolon behind the Terminator identifier, as shown in the previous code. If you use Heredoc in a more complex expression, you need to write an 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 a eyebrow. "Want to bet?"
No_more;
Echo $dialogue;
"It ' s not going to happen!" she fumed.
He raised a eyebrow. "Want to bet?"
Whitespace characters in Heredoc are also preserved:
$ws = <<< Enough
Boo
Hoo
enough;
$ws = "boo\n hoo\n";
Because the line breaks before the end Terminator are removed, the following two assignments are the same:
$s = ' Foo ';
Same as is the same as the following
$s = <<< End_of_pointless_heredoc
Foo
End_of_pointless_heredoc;
If you want to use a newline character to end a Heredoc-referenced string, you will need to add extra:
$s = <<< End
Foo

End;
Note Foo is followed by a blank line and cannot be deleted

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.