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:
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:
But using constants within quotation marks is more complicated. You cannot print constants within single or double quotation marks, like this:
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:
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:
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).
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.
- To begin, create a new PHP document, beginning with the initial PHP tag (Script 8-5):
- Address error handling, if necessary:
- Define the page title as a constant:
Here you define one constant, TITLE, and give it the value Records by Elliott Smith. - Include the header file:
This script uses the same header file as all the others, although you will modify it shortly to take the constant into account. - Close the PHP section and create the HTML:
The content here is simple but serves the page’s purpose nicely. - Create a new PHP section that includes the footer file:
- 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:
- Using Synapse to connect to the production server, open header.html (Script 8-2) in Brackets.
- 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.
- In the place of the deleted text (between the title tags), add the following (Script 8-6):
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. - 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.
- 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.
- Test albums.php in your browser (Figure 8-8).
- View index.php (the home page) in your browser (Figure 8-9).
- If you want, add the constant definition line to index.php.
- 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.
- 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.