Difference between revisions of "PHP Constants"
m (Text replacement - "<google>BUY_PHP</google>" to "<htmlet>php<htmlet>") |
m (Text replacement - "<google>BUY_PHP_BOTTOM</google>" to "<htmlet>php<htmlet>") |
||
Line 189: | Line 189: | ||
</table> | </table> | ||
− | < | + | <htmlet>php<htmlet> |
Revision as of 22:21, 1 February 2016
Previous | Table of Contents | Next |
Understanding PHP Variable Types | PHP Operators |
The key point to understand here as that the argument passed through to constant() can be a string variable set to the name of the constant.
As always an example helps a great deal in understanding a concept. In the script below we define a constant called MY_CONSTANT. Next, we create a string variable called constantName and assign it a value of MY_CONSTANT (i.e. a string that matches the constant name). We can then use this new variable as the argument to constant() to obtain the constant value of MY_CONSTANT:
<?php define ('MY_CONSTANT', "This is a constant string."); $constantName = 'MY_CONSTANT'; if (defined ( $constantName )) { echo constant($constantName); } else { echo "$constantName constant is not defined."; } ?>
The above script will display the constant value if it exists by using the value of the $constantName variable constant name key.
Predefined PHP Constants
As we mentioned briefly at the start of this chapter, PHP provides a number of built-in constants that can be of significant use to the PHP web developer. In this section we will look at some of the more useful constants available.
PHP Script and Environment Related Constants
The following constants provide information about the script which is currently executing, and also the environment in which the PHP web server module is running. These are of particular use when debugging scripts:
Constant Name | Description |
---|---|
__LINE__ | Contains the number of the line in the current PHP file (or include file) which is being currently being executed by the PHP pre-processor. |
__FILE__ | Contains the name of the file or include which contains the currently executing line of PHP code. |
__FUNCTION__ | Contains the name of the PHP function which is currently executing |
__CLASS__ | Contains the class which is currently in use |
__METHOD__ | Contains the name of the method in the current class which is currently executing |
PHP_VERSION | Contains the version of PHP that is executing the script |
PHP_OS | Contains of the name of the Operating System hosting the PHP Pre-processor |
PHP_EOL | Contains the Newline character for the host OS (differs between UNIX/Linux and Windows for example) |
DEFAULT_INCLUDE_PATH | The default path where PHP looks for include files |
PHP Mathematical Constants
PHP provides a number of useful mathematical constants that can be used to save both programming time when writing a script, and processing time when performing calculations in a script. The following table provides a list of the mathematical constants available in PHP:
Constant | Description |
---|---|
M_E | Value of e |
M_EULER | Value of Euler's constant |
M_LNPI | The natural logarithm of PI |
M_LN2 | The natural logarithm of 2 |
M_LN10 | The natural logarithm of 10 |
M_LOG2E | Value of base-2 logarithm of E |
M_LOG10E | The base-10 logarithm of E |
M_PI | The value of PI |
M_PI_2 | The value of PI/2 |
M_PI_4 | The value of PI/4 |
M_1_PI | The value of 1/PI |
M_2_PI | The value of 2/PI |
M_SQRTPI | The square root of PI |
M_2_SQRTPI | The value 2/square root of PI |
M_SQRT2 | The square root of 2 |
M_SQRT3 | The square root of 3 |
M_SQRT1_2 | The square root of 1/2 |