The Building Blocks: Data Types, Literals, Variables, and Constants - Part 3/Page 2
[previous]
The Building Blocks: Data Types, Literals, Variables, and Constants - Part 3
The constant() Function
The constant()
function returns that value of a constant. This function can be helpful if you don't know the name of the constant because its name was stored in a variable or was returned from a function.
Format
Predefined and "Magic" Constants
PHP comes with a number of predefined constants shown in Table 4.6. They provide information that doesn't change such as the name of the script file, the version of PHP and the operating system, and so on.
There are five predefined constants called magic constants (see Table 4.7). These are constants that change depending on how they are used in a program. They cannot be enclosed in quotes and are not case sensitive. The name of the constant is enclosed in two underscores on both sides.
Table 4.7 Magic Constants
Name | Description |
__LINE__ | The current line number of the file. |
__FILE__ | The full path and filename of the file. If used inside an include, the name of the included file is returned. |
__FUNCTION__ | The function name (added in PHP 4.3.0). As of PHP 5 this constant returns the function name as it was declared (case sensitive). In PHP 4 its value is always lowercased. |
__CLASS__ | The class name (added in PHP 4.3.0). As of PHP 5 this constant returns the class name as it was declared (case sensitive). In PHP 4 its value is always lowercased. |
__METHOD__ | The class method name (added in PHP 5.0.0). The method name is returned as it was declared (case sensitive). |
Constants 103
PHP has several special built-in constants described in Table 4.8.
Table 4.8 Built-In Constants
Name | Description |
PHP_VERSION | The version of the PHP parser currently running |
PHP_OS | The operating system of the server on which the PHP parser is running |
PHP_OS | The name of the operating system on which the PHP parser is executing; e.g., Linux |
TRUE | A true value. |
FALSE | A false value. |
The script in Example 4.26 shows how the predefined constants can be used to give information to the browser. It's output is displayed in Figure 4.31.
Example 4.26
This chapter is excerpted from the book titled, PHP and MySQL by Example, authored by Ellie Quigley. Copyright 2007 Pearson Education, Inc., published by Prentice Hall Professional, November, 2006. ISBN 0131875086.
[previous]
URL: