Changes

PHP Constants

9 bytes removed, 14:24, 13 May 2009
no edit summary
If you look up the word constant in a dictionary it will probably tell you that the word is used to describe something that is non-changing and non-variable, and this is exactly the purpose of constants in PHP. A PHP constatnt constant is the opposite of a variable in that once it has been defined it cannot be changed.
Constants are particularly useful for defining a value that you frequently need to refer to that does not ever change. For example, you might define a constant called INCHES_PER_YARD that contains the number of inches in a yard. Since this is a value that typically doesn't from one day to the next it makes sense to define it as a constant. Conversely, a value that is likely to change, such as the Dollar to Yen exchange rate is best defined as a variable.
The define function takes two arguments, the first being the name you wish to assign to the constant, and the second the value to assign to that name.
Constant name names are case sensitive. Although it is not a requirement, convention carried over from other programming languages suggests that constants should be named in all upper case characters. The following example demonstrates the use of the ''define()'' function to specify a constant:
<pre>
== Checking if a PHP Constant is Defined ==
It can often be useful to find out if a constant is actually defined. This can be achieved using the PHP ''defined()'' function. The ''defined()'' function takes the name of the constant to be checked as an argument and returns a value of ''true'' or ''false'' (i.e 1 or 0) to indicate whether that constant exists.
For example, let's assume we wish to find out if a constant named ''MY_CONSTANT'' is defined. We can simply call the ''defined()'' function passing through the name, and then test the result using an ''if .. else'' statement (see [[PHP Flow Control and Looping]] for more details on using ''if .. else''):
== Using a Variable as a Constant Name ==
<google>ADSDAQBOX_FLOW</google>
It is not always the case that you want to ''hard-code'' a constant name into a script at the point you wish to access it. For example, you may have a general purpose script that you wish to perform tasks on any number of different constants, not just one that you happen to have typed in the name for. The best way to resolve this issue is store the name of the constant in a variable. How then, would you access the value assigned to that constant? The answer is to use the PHP ''constant()'' function. The ''constant()'' function takes the name of the constant as an argument and returns the value of the constant which matches that name.
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 matrches matches the constant name). We can then use this new variable as the argument to ''constant()'' to obtain the constant value of ''MY_CONSTANT'':
<pre>
<table border="1" cellspacing="0">
<tr style="background:#efefef;">
<th>Constant Name<th>DescritionDescription</th>
<tr>
<td>__LINE__<td>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.</td>
<td>__FUNCTION__<td>Contains the name of the PHP function which is currently executing</td>
<tr>
<td>__CLASS__<td>Contains the current class which is currently in use</td>
<tr>
<td>__METHOD__<td>Contains the name of the method in the curerent current class which is currently executing</td>
<tr>
<td>PHP_VERSION<td>Contains the version of PHP that is executing the script</td>
<td>PHP_OS<td>Contains of the name of the Operating System hosting the PHP Pre-processor</td>
<tr>
<td>PHP_EOL<td>Contains the Newline chacter character for the host OS (differs between UNIX/Linux and Windows for example)</td>
<tr>
<td>DEFAULT_INCLUDE_PATH<td>The default path where PHP looks for include files</td>