About the PHP constants

The reader of the PHP cert guide might be wondering about some aspects of the PHP constants, because the constants section of the book doesn't go into as much detail as it goes for other sections like variables for example.

1. It's not necassary to name the constants always in upper case. It is, however, a good practice, kind of an unspoken convension. You'll make your code easier to read (and maintain) if you always use upper case.

2. You don't have to use double quotes when defining a constant. In fact, when you don't have a reason to use double quotes, always use single quotes, it's better in terms of performance.

En example to illustrate the two points above:
< ?php
define ('something', 123);
echo something;
?>

3. Constants names are case-sensitive. So the following will not work as expected:
< ?php
define ('something', 123);
echo someThing;
?>

4. You cannot include a constant's value as part of a string as you can do with a variable. Here's an illustration:
< ?php
$var = 'test';
define ('CONST', 123);
echo "testing $var variable";
echo "testing {$var} variable";
echo "testing SOMETHING constant";
echo "testing {SOMETHING} constant";
?>

This will result in:
testing test variable
testing test variable
testing SOMETHING constant
testing {SOMETHING} constant

This entry was posted on Monday, March 7th, 2005 and is filed under PHP certification. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

4 Responses to “About the PHP constants”

  1. Stoyan Says:

    Also, a good idea before defining a constant is to check if this constant was already defined, like:

    < ?php
    if (!defined('CONST')) {
    define('CONST', 'some value here');
    }
    ?>

    This way you can avoid any warning messages if this constant was already defined.

  2. Stoyan Says:

    Some applications are using constants as a protection tool, to make sure that files meant to be included are not accessed directly. Such application is for example phpBB.

    Let’s say you have a main file index.php which includes inc.me.php from the same directory. So far there’s nothing that prevents somebody from typing inc.me.php in the browser and accessing the file directly. Here comes the trick:
    1. In index.php you define a constant
    2. In inc.me.php you check if this constant is defined. If it isn’t then this file accessed directly. You can die() right there ;)

    code in index.php
    define('INCLUDE_CONSTANT', true);

    code in inc.me.php, preferable somewhere in the beginning of the file
    if (!defined('INCLUDE_CONSTANT') || !INCLUDE_CONSTANT) {
    die('Sorry!');
    }

  3. Denis de Bernardy Says:

    Shouldn’t you be testing:


    <?php
    if (!defined('CONST')) {
    die('check code for irrelevant include');
    }
    ?>

  4. Theodore Pride Says:

    If you use include_once or require_once (always a good idea for code libraries, imho), then you won’t get double constant declarations, so the point is moot.

Leave a Reply