PHP/String/Quotation
Содержание
- 1 Double quotation marks allow the parsing of variables
- 2 Double-quoted string escape sequences
- 3 Double-quoted strings
- 4 Fixing quote escaping in backreference replacements
- 5 If you use single quotation marks to enclose the same string, the variable is not substituted:
- 6 It is safe to use non-escaped Windows-style filenames in your single-quoted strings
- 7 Using forward slashes on Windows
- 8 Various special characters in string assignments
Double quotation marks allow the parsing of variables
<?
$name = "john";
print "hello, $name";
?>
Double-quoted string escape sequences
Escape sequence Character
\n Newline (ASCII 10)
\r Carriage return (ASCII 13)
\t Tab (ASCII 9)
\\ Backslash
\$ Dollar sign
\" Double quotes
\0 through \777 Octal value
\x0 through \xFF Hex value
Double-quoted strings
<?
print "I"ve gone to the store.";
print "The sauce cost \$10.25.";
$cost = "$10.25";
print "The sauce cost $cost.";
print "The sauce cost \$\061\060.\x32\x35.";
?>
Fixing quote escaping in backreference replacements
<?php
$html = "<code><b> It"s bold </b></code>";
print preg_replace("@<code>(.*?)</code>@e","preg_html_entity_decode("$1")", $html);
print "\n";
$html = "<code><i> "This" is italic. </i></code>";
print preg_replace("@<code>(.*?)</code>@e","preg_html_entity_decode("$1")", $html);
print "\n";
function preg_html_entity_decode($s) {
$s = str_replace("\\"",""", $s);
return html_entity_decode($s);
}
?>
If you use single quotation marks to enclose the same string, the variable is not substituted:
<?
print "hello, $name"; // hello, $name
?>
Double-quoted strings are also parsed for escape characters.
It is safe to use non-escaped Windows-style filenames in your single-quoted strings
<?php
$filename = "c:\windows\me.txt";
echo $filename;
?>
Using forward slashes on Windows
<?php
$fh = fopen("c:/alligator/crocodile menu.txt","r") or die($php_errormsg);
while($s = fgets($fh)) {
print $s;
}
fclose($fh) or die($php_errormsg);
?>
Various special characters in string assignments
<?php
$newline = "A newline is \n";
$return = "A carriage return is \r";
$tab = "A tab is \t";
$dollar = "A dollar sign is \$";
$doublequote = "A double-quote is \"";
?>