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).




    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