|
PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide Authors: Ullman L Published year: 2005 Pages: 21-22/166 |
About ConstantsConstants 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
|
Single vs. Double Quotation MarksIn 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.
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
|
||||||||||||||||||||||||||||||
|
PHP and MySQL for Dynamic Web Sites. Visual QuickPro Guide Authors: Ullman L Published year: 2005 Pages: 21-22/166 |
![]() PHP for the World Wide Web, Second Edition | ![]() Beginning PHP and MySQL 5: From Novice to Professional | ![]() The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP |