Chapter 8 Exercises

Exercise 3: Using Constants

Constants are a specific data type in PHP. Unlike variables, they retain their initial value throughout the course of a script. You cannot change the value of a constant once it has been set.

To create a constant, use the define() function instead of the assignment operator (=) as used for variables:

The PHP code to define a constant.

Notice that—as a rule of thumb—constants are named using all capital letters, although this is not mandatory. Most important, constants do not use the initial dollar sign as variables do (because constants are not variables).

Referring to constants is generally straightforward:

The PHP code that uses a constant.

But using constants within quotation marks is more complicated. You cannot print constants within single or double quotation marks, like this:

The PHP code that contains constants in quotations.

In both cases, the literal text Hello, FIRST_NAME will be sent to the browser. The solution is to use concatenation or multiple print() statements:

The PHP code that contains constants without quotations.

Not to confuse you, but along with the define() function for making constants is the defined() function, which returns TRUE if the submitted constant has been defined:

The PHP code that checks if the constants are defined or not.


In this exercise you will give your web application the ability to display a different title for each page (which appears at the top of the browser window). To accomplish this, you will define a constant in the parent script that is printed by the header file. This technique works because any variable or constant that exists in the parent document before the include() or require() call are available to the included file (so that it is as if the included file was part of the parent file).

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

To use constants:

Script 8-5: This script uses the same template system as index.php (Script 8-4) but also uses a constant to identify the page’s title.
The PHP script that uses constants as displayed in the editor view.

  1. To begin, create a new PHP document, beginning with the initial PHP tag (Script 8-5):
    The PHP opening tag as displayed in the editor view.
  2. Address error handling, if necessary:
    The PHP code snippets for error handling as displayed in the editor view.
  3. Define the page title as a constant:
    The PHP code that defines a constant as displayed in the editor view.
    Here you define one constant, TITLE, and give it the value Records by Elliott Smith.
  4. Include the header file:
    The PHP code that includes the header file as displayed in the editor view.
    This script uses the same header file as all the others, although you will modify it shortly to take the constant into account.
  5. Close the PHP section and create the HTML:
    The code snippet that closes the PHP section and creates the HTML as displayed in the editor view.
    The content here is simple but serves the page’s purpose nicely.
  6. Create a new PHP section that includes the footer file:
    A new PHP section that includes the footer file as displayed in the editor view.
  7. Save the file as albums.php and upload to the production server at studentuploads.ca to the chapter8 directory (https://www.studentuploads.ca /firstl/202/chapter8).

To take advantage of the constant, you now need to rewrite the header.html file.

To print out a constant:

  1. Using Synapse to connect to the production server, open header.html (Script 8-2) in Brackets.
  2. Delete the Elliott Smith Fan Club text that appears between the title tags.
    Now that the page title will be determined on a page-by-page basis, you do not need it to be hard-coded into the page.
    The PHP code that automatically adds the title to the HTML page as displayed in the editor view.
  3. In the place of the deleted text (between the title tags), add the following (Script 8-6):
    The PHP code that creates a title for the page as displayed in the editor view.
    To have PHP create the page title, you need to begin by starting a section of PHP code between the title tags. Then you use a conditional to see if the TITLE constant has been defined. If it has, you print its value as the page title. If TITLE has not been defined, you print a default title.
  4. Save the file as header.html. Since you have opened this page straight from the production server using Synapse, your changes will be applied right on the production server by simply pressing Ctrl-S (Cmd-S on a Mac). This means that you don’t need to use FTP.
  5. Switch over to your browser, and test the file there. Make sure you are looking at the production server version of the file, and that you have refreshed to ensure you are looking at the latest iteration.
  6. Test albums.php in your browser (Figure 8-8).
    The result that shows Albums as displayed in the browser view.
  7. View index.php (the home page) in your browser (Figure 8-9).
    The result after running the PHP code as displayed in the browser view.
  8. If you want, add the constant definition line to index.php.
  9. If your file needs editing, you can continue editing and saving your changes in Brackets, then just switching to your browser and refreshing the page. Since you have opened this file straight from the production server using Synapse, your changes will be applied right on the production server. This means that you don’t need to use FTP.

 

Quick tips icon.

  • The formal rules for naming constants are exactly like those for variables except for the omission of a dollar sign. Constant names must begin with a letter; can contain any combination of letters, numbers, and the underscore; and are case-sensitive.
  • To be frank, this is a trivial use of a constant (a variable would have worked just as well for setting the page title), but it was a good way to demonstrate the idea to you. A good use of constants is for establishing your database connection information.
  • PHP runs with several predefined constants. These include PHP_VERSION (the version of PHP running) and PHP_OS (the operating system of the server).
  • In Chapter 9, Cookies and Sessions, you will learn about another constant, SID (which stands for session ID).
  • An added benefit of using constants is that they are global in scope. This concept will mean more to you after you read the Chapter 10, Exercise 5: Understanding Variable Scope.
  • Be aware that the value of a constant can never be changed, and a constant cannot be deleted. Also, unlike arrays, a constant can only contain a single value like a string or a number variable.

Return to Chapter 8, Assignments Page