- A constant is a identifier or name for a simple value. As the name suggests, that value cannot change during the execution of the script.
- A constant identifier is case-sensitive by default.
- By convention constant identifiers are always uppercase.
- The scope of a constant is global -- you can access it anywhere in your script without regard to scope.
- You define a constant by using the define() function.
- Once a constant is defined, it can never be changed or undefined.
- Only scalar data (boolean, integer, float and string, not arrays) can be contained in constants.
<?php define("PI", 3.14159 ); $radius = 6378.14 ; // mean equitorial radius of the earth in kilometers $volume = ( 4 / 3 ) * PI * $radius^3 ; // Calculate the approximate volume of the earth echo "Radius: $radius km" ; echo "Volume: $volume cubic kilometers" ; ?>Since names for constants can be made from any letters (a to z and A to Z) and from ASCII characters 128 through 255, you can name constants as follows (if you wanted to):
define("¢", "Cents") ; // ¢ = ASCII 155 define("£", "Pounds Sterling") ; // £ = ASCII 156 echo "Do you want that in " . £ . " or in Dollars and " . ¢ . "?" ;
Copyright © 2003
Henry H. Hartley
All Rights Reserved