PHP/String/Quotation — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 07:07, 26 мая 2010

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>&lt;b&gt; It"s bold &lt;/b&gt;</code>";
print preg_replace("@<code>(.*?)</code>@e","preg_html_entity_decode("$1")", $html);
print "\n";
$html = "<code>&lt;i&gt; "This" is italic. &lt;/i&gt;</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 \"";
?>