Single vs. Double Quotation Marks


In PHP it's important to understand how single quotation marks differ from double quotation marks. With the echo() and print() statements you can use either, as in the examples in this chapter. But there is a key difference between the two and why you might use them. So far I've specified when you should use which, but now it's time to define the pattern more explicitly.

In PHP, values enclosed within single quotation marks will be treated literally, whereas those within double quotation marks will be interpreted. In other words, placing variables and special characters (Table 1.2) within double quotes will result in their represented values printed, not their literal values. For example, assume that you have

 $var = 'test'; 

Table 1.2. These characters have special meanings when used within double quotation marks.

Escaped Characters

CODE

MEANING

\"

Double quotation mark

\'

Single quotation mark

\\

Backslash

\n

Newline

\r

Carriage return

\t

Tab

\$

Dollar sign


The code echo "var is equal to $var"; will print out var is equal to test, whereas the code echo 'var is equal to $var'; will print out var is equal to $var. Using an escaped dollar sign, the code echo "\$var is equal to $var"; will print out $var is equal to test, whereas the code echo '\$var is equal to $var'; will print out \$var is equal to $var.

As these examples should illustrate, double quotation marks will replace a variable's name ($var) with its value (test) and a special character's code (\$) with its represented value ($). Single quotes will always display exactly what you type, except for the escaped single quote (\') and the escaped backslash (\\), which are printed as a single quotation mark and a single backslash, respectively.

The preceding rule applies to any use of quotation marks, be it in an echo() or print() statement, when assigning a value to a variable, or sending data to a function as an argument. As another example of how the two quotation marks differ, I'll modify the numbers.php script as an experiment.

To use single and double quotation marks

1.

Open numbers.php (refer to Script 1.9) in your text editor.

2.

Delete the existing echo() statement (Script 1.11).

Script 1.11. This final script demonstrates the differences between using single and double quotation marks.


3.

Print a caption and then rewrite the original echo() statement using double quotation marks.

 echo 'Using double quotation marks:  <br />'; echo "You are purchasing  <b>$quantity</b> widget(s) at  a cost of <b>\$$price</b> each.  With tax, the total comes to  <b>\$$total</b>.\n; 

The same intended result of this scriptprinting the quantity of widgets purchased, at what price, and what the total amount is with taxcan be achieved by using double quotation marks instead of single quotation marks and the concatenation operator, as it had originally. Notice that I've also used the \$$variable technique to generate results like $3778.43 (the first dollar sign is printed and the second is the start of the variable name).

4.

Repeat the first echo() statement, this time using single quotation marks.

 echo '<p><hr /></p>Using single  quotation marks:<br />'; echo 'You are purchasing  <b>$quantity</b> widget(s) at  a cost of <b>\$$price</b> each.  With tax, the total comes to  <b>\$$total</b>.\n; 

The first line adds some spacing and a horizontal rule, followed by a caption. The second echo() statement is used to highlight the difference between using single or double quotation marks.

5.

If you want, change the page's title.

6.

Save the file as quotes.php, upload to your Web server, and test in your Web browser (Figure 1.22).

Figure 1.22. These results demonstrate when and how you'd use one type of quotation mark as opposed to the other.


Tips

  • Because PHP will attempt to find variables whose values need to be inserted within double quotation marks, using single quotation marks is theoretically faster. If you need to print the value of a variable, though, you must use double quotation marks.

  • As valid HTML often includes a lot of double-quoted attributes, it's often easiest to use single quotation marks when printing HTML with PHP.

     echo '<table width="80%" border="0"  cellspacing="2 cellpadding="3"  align="center>'; 

    If you were to print out this HTML using double quotation marks, you would have to escape all of the double quotation marks in the string.

     echo "<table width=\"80%\"  border=\"0\" cellspacing=\"2\"  cellpadding=\"3\" align=  \"center\">"; 

  • PHP also supports the heredoc method of quoting text. If you are already familiar with the concept or are curious about alternative methods, check the PHP manual for the proper syntax.




    PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
    PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    ISBN: 0321336577
    EAN: 2147483647
    Year: 2005
    Pages: 166
    Authors: Larry Ullman

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net