PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide
Authors: Ullman L
Published year: 2005
Pages: 21-22/166
Buy this book on amazon.com >>

About Constants

Constants are a specific data type in PHP that, unlike variables , retain their initial value throughout the course of a script. In fact, you cannot change the value of a constant once it has been set. Constants can be assigned any single valuea number or a string of characters .

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

define ('NAME', 'value');

Notice that, as a rule of thumb, constants are named using all capitals, although this is not required. Most importantly, constants do not use the initial dollar sign as variables do (because, technically, constants are not variables).

Printing constants requires special syntax as well:

define ('USERNAME', 'trout');

echo 'Hello, ' . USERNAME;

You cannot print constants using echo " Hello, USERNAME ", as PHP would just print Hello, USERNAME and not the value of the USERNAME constant (because there's no dollar sign telling PHP that USERNAME is anything other than literal text).

PHP runs with several predefined constants, much like the predefined variables used earlier in the chapter. These include PHP_VERSION (the version of PHP running) and PHP_OS (the operating system of the server).

To use constants

1.

Create a new PHP document in your text editor ( Script 1.10 ).

<!DOCTYPE html PUBLIC "-//W3C//

DTD XHTML 1.0 Transitional//EN
"http://www.w3.org/TR/xhtml1/DTD/

xhtml1-transitional.dtd>
<html xmlns="http://www.w3.org/1999/

xhtml xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type"

content="text/html;

charset=iso-8859-1 />
  <title>Constants</title>
</head>
<body>
<?php # Script 1.10 - constants.php

Script 1.10. Constants are another data type you can use in PHP, distinct from variables.


2.

Create a new date constant.

define ('TODAY', 'February 3, 2005');

An admittedly trivial use of constants, but this example will illustrate the point. In Chapter 7, "Using PHP with MySQL," you'll see how to use constants to store your database access information.

3.

Print out the date, the PHP version, and operating system information.

echo 'Today is ' . TODAY . '.<br

/>This server is running version

<b>' . PHP_VERSION . '</b> of PHP

on the <b>' . PHP_OS . '</b>

operating system.';

Since constants cannot be printed within quotation marks (the lack of a dollar sign causes them to be treated as capitalized text), I use the concatenation operator to create my echo() statement.

4.

Complete the PHP code and the HTML page.

?>
</body>
</html>

5.

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

Figure 1.20. By making use of PHP's constants, you can learn more about your PHP setup.


Tips

  • If possible, run this script on another PHP-enabled server ( Figure 1.21 ).

    Figure 1.21. Running the same script (refer to Script 1.10) on different servers garners different results.


  • In Chapter 9, "Cookies and Sessions," you'll learn about another constant, SID (which stands for session ID ).



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

C ODE

M EANING

\"

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 78.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
Authors: Ullman L
Published year: 2005
Pages: 21-22/166
Buy this book on amazon.com >>

Similar books on Amazon