PHP/String/Quotation

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

Double quotation marks allow the parsing of variables

   <source lang="html4strict">

<? $name = "john"; print "hello, $name"; ?>

 </source>
   
  


Double-quoted string escape sequences

   <source lang="html4strict">

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

 </source>
   
  


Double-quoted strings

   <source lang="html4strict">

<? 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."; ?>

 </source>
   
  


Fixing quote escaping in backreference replacements

   <source lang="html4strict">

<?php $html = "<b> It"s bold </b>"; print preg_replace("@(.*?)@e","preg_html_entity_decode("$1")", $html); print "\n"; $html = "<i> "This" is italic. </i>"; print preg_replace("@(.*?)@e","preg_html_entity_decode("$1")", $html); print "\n"; function preg_html_entity_decode($s) {

   $s = str_replace("\\"",""", $s);
   return html_entity_decode($s);

} ?>

 </source>
   
  


If you use single quotation marks to enclose the same string, the variable is not substituted:

   <source lang="html4strict">

<? print "hello, $name"; // hello, $name ?> Double-quoted strings are also parsed for escape characters.

 </source>
   
  


It is safe to use non-escaped Windows-style filenames in your single-quoted strings

   <source lang="html4strict">

<?php

           $filename = "c:\windows\me.txt";
           echo $filename;
   ?>
 
 </source>
   
  


Using forward slashes on Windows

   <source lang="html4strict">

<?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); ?>

 </source>
   
  


Various special characters in string assignments

   <source lang="html4strict">

<?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 \""; ?>

 </source>