You may bump into the following notice when you’re coding PHP, whereas “DEFINED_CONSTANTS” could be another constant you are trying to call.
However, this notice actually means that you are trying to call a undefined constant. In this case we are trying to call the constant “DEFINED_CONSTANTS”, which has not been defined. How did this happen ?
<?php
// Define constant "DEFINED_CONSTANT"
define('DEFINED_CONSTANT', 'this is the constant value');
// echo a undefined constant (just one letter to much) - this causes the notice
echo DEFINED_CONSTANTS;
// Just a line break
echo '<br />';
// echo the defined constant, which has actually been defined
echo DEFINED_CONSTANT;
?>
This will be the result:
DEFINED_CONSTANTS
this is the constant value
As you can see, a constant needs to be defined with the PHP function “define“. In this example we defined the constant “DEFINED_CONSTANT” instead of the constant we are trying to call “DEFINED_CONSTANTS”. We just had a little typo, one “S” to much on the end. When we remove the “S” on the end, it will actually work, as that specific constant has been defined.
<?php
// Define constant
define('DEFINED_CONSTANT', 'this is the constant value');
// echo the defined constant
echo DEFINED_CONSTANT;
?>
The above example results in:
The only thing you have to do, is finding the function “define” which point to the constant which have been actually defined. If there are way to much constants defined, and you don’t know where to look, just run the following piece of PHP code to check all the user defined constants.
<?php
// Define constant
define('DEFINED_CONSTANT', 'this is the constant value');
// echo the defined constant
echo DEFINED_CONSTANT;
// Get all the defined constants
$user_defined_constants = get_defined_constants(true);
// Get the user defined constants
echo '<pre>'.print_r($user_defined_constants['user'], true).'</pre>';
?>
The above example will result in: this is the constant value
Array
(
[DEFINED_CONSTANT] => this is the constant value
)
As you can see, all defined constants will be shown in a simple array, whereas the key will present the constant defined and the array value the constant value. Now you have a little clue on which constant you are looking for.
If you wrote the PHP code youself, you may use the PHP function “defined” to check whether the constant has been defined or not.
<?php
// Define constant
define('DEFINED_CONSTANT', 'this is the constant value');
// echo a undefined constant (just one letter more) - but first check if the constant has been defined
if (defined('DEFINED_CONSTANTS'))
echo DEFINED_CONSTANTS;
else
echo 'Undefined constant';
// Just a line break
echo '<br />';
// echo the defined constant
echo DEFINED_CONSTANT;
?>
The above example will result in (Without any ugly notices or errors):
this is the constant value
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)

» Latest comments