Chapter 2 Exercises

Exercise 5: Understanding Quotation Marks

Now that you have some basic information about variables and know how to create them, let's turn to the important concept of quotation marks. PHP, like most programming languages, supports both double (“) and single (‘) quotation marks—but each gives entirely different results.

The rule of thumb is: items within single quotation marks are treated literally, while items within double quotation marks are extrapolated (that is, a variable’s name is replaced with its value, as you saw in Script 2-3). This rule applies anywhere in PHP you may use quotation marks, including the creation of string variables and using the print() function. Working through an example is the best way to learn.


In this exercise you will work with both single and double quotation marks to see the difference in action.

Note: A number of Helpful Tips are located at the end of this web page for reference as you complete this exercise.

To use quotation marks:

Script 2-4: The type of quotation mark you use with variables is vital to the end result.

The example of a script that uses single or double quotation as displayed on the editor view.

  1. To begin, create a new PHP script starting with the HTML code with the title Quotes (Script 2-4).
  2. Create the initial HTML tags:

    The initial HTML tags with the title Quotes as displayed in the editor view.

  3. Begin the PHP code:
    The PHP start tag in the editor view.
  4. Create two string variables:
    The examples that contain single or double quotation marks as displayed in the editor view.
    It does not matter whether you use single or double quotation marks, as each string should be treated literally.
  5. Create two different name variables, using the first_name and last_name variables:
    The script that creates two different name variables using first_ and last_ name variables as displayed in the editor view.
    In these lines it makes a huge difference which quotation marks you use. The $name1 variable is literally equal to $first_name $last_name, because no extrapolation occurs. On the other hand, $name2 is now equal to John Smith, presumably the result you are after.
  6. Print out the variables using both types of quotation marks:
    The script that prints out the variables using both types of quotation marks as displayed in the editor view.
    Again, the quotation marks make all the difference. The first print() statement prints out the values of the $name1 and $name2 variables, whereas the second prints out $name1 and $name2.
    The HTML in the print() statements makes them more legible in the browser, and each statement is executed over two lines, which is perfectly acceptable.
  7. Complete the PHP section and the HTML page:
    The PHP, HTML close tag as displayed in the editor view.
  8. Save the file as quotes.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter2).
  9. Using Cyberduck, upload the file to the production server, and test in your browser (Figure 2-8), making sure you are looking at the production server version of the file.
    The result of Quotes.php as displayed in the browser view.
Important: If something doesn’t look right in your document, and you need to make changes to the script, follow the editing procedure using Synapse outlined in Chapter 1.

 

Quick tips icon.

  • If you are feeling confused about the distinction between single and double quotation marks, stick with using double quotation marks and you will be safe.
  • Arguably, using single quotation marks when you can is preferable, as PHP won’t need to search the strings looking for variables. This rule is more of a finesse issue—performance will not be measurably affected regardless.
  • The shortcuts for creating newlines (\n), tabs (\t), and double quotation marks (\”) must also be used within double quotation marks to have the desired effect.
  • You can also print a variable’s value without using quotation marks, assuming you print nothing else:
    The print variable in PHP.

Return to Chapter 2, Assignments Page