| home / programming / php / by_example3 |
|
|
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.
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.
| 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). |
PHP has several special built-in constants described in Table 4.8.
| 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.
| home / programming / php / by_example2 |
URL: