PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousVariables from outside PHPPredefined constantsnext
Last updated: Tue, 28 May 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

Luku 8. Constants

A constant is a identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script. (The 'magic constants' __FILE__ and __LINE__ appear to be an exception to this, but they're not actually constants.) A constant is case-sensitive by default. By convention constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Huomaa: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).

The scope of a constant is global--you can access it anywhere in your script without regard to scope.

Syntax

You can define a constant by using the define()-function. Once a constant is defined, it can never be changed or undefined.

Only scalar data (boolean, integer, double and string) can be contained in constants.

You can get the value of a constant by simply specifying its name. Unlike with variables, you should not prepend a constant with a $. You can also use the function constant(), to read a constant's value, if you are to obtain the constant's name dynamically. Use get_defined_constants() to get a list of all defined constants.

Huomaa: Constants and (global) variables are in a different namespace. This implies that for example TRUE and $TRUE are generally different.

If you use an undefined constant, PHP assumes that you mean the name of the constant itself. A notice will be issued when this happens. Use the defined()-function if you want to know if a constant is set.

These are the differences between constants and variables:

  • Constants do not have a dollar sign ($) before them;

  • Constants may only be defined using the define() function, not by simple assignment;

  • Constants may be defined and accessed anywhere without regard to variable scoping rules;

  • Constants may not be redefined or undefined once they have been set; and

  • Constants may only evaluate to scalar values.

Esimerkki 8-1. Defining Constants

<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.
?>

User Contributed Notes
Constants
add a note about notes

29-Jan-2000 03:13

You can't define a constant that is another constant.  In the same versions
where define('FOO', TRUE) won't work, neither will define ('BAR',
__LINE__).  At least one of the DOS versions of php3 reliably demonstrates
this behaviour.  I'd call it a "no-no" to try and define a
constant based on another constant.


04-Aug-2000 11:44

To get a full path (the equivalent of something like "__PATH__")
use 
dirname($SCRIPT_FILENAME)
to get the directory name of the called script and
dirname(__FILE__)
to get the directory name of the include file.


23-Jan-2001 11:54

It may be useful to note that, in php4 (what version this started I don't
know, but it didn't do it before we upgraded to php4) __FILE__ will follow
symlinks to the origional file.


09-Feb-2001 01:23

"SID" is defined also. It's the same as
"session_name()=session_id()" and usufull for managing session
using URL.
It's mentioned in Session reference, but I thought it would appropriate
mention this section, too.

"NULL" is also defined and has null type. gettype(null) returns
null and is_null($val) returns true for null - PHP4.0.4pl1 or later(?)

(I would like to know ALL constants defined by PHP in one place)

I also think that the class initilizer limitation intoruduced in PHP4
should be noted here. 

PHP4 does not allow;

define ('C1',1);
define ('C2',2);
class foo {
  var $val = C1 | C2;
}

Following code works.(PHP4.0.4pl1-linux)
define ('C1',1);
define ('C2',2);
class foo {
  var $val = C1;
  functin foo() {
    $this->val = C1 | C2;
  }
}

Details are explained 

(thanks to hholzgra)


31-Mar-2001 04:38

If you use $PHP_SELF in forms or in places where a script has to reload
itself, you will get the full path relative to the web root. This is very
useful because it will always work, even if you move your script around on
the server. You don't need to mess around with absolute or relative paths.
An example would be:

<form method=POST action=$PHP_SELF?do_something> or
<a href=$PHP_SELF?do_something>Make script do something</a>


11-Jun-2001 04:42

The pre-defined constant '__FILE__' does not work in same way at every
version of PHP.

Some version of PHP has the relative path, and some other has the absolute
path on __FILE__ constant..

Please be carefull in use..

[PS]
I have not tested at all versions of PHP but the version of 4.04pl.. and
4.05 are certainly not working in same way..  If you want to see that
bug(?), I can show you an example.


22-Jun-2001 05:52

Constants have the same naming rules as variables (though the docs don't
actually say that). So you can't begin a constant name with a number.

if you name the
constant beginning with a numeral, such as:

define ("999CONSTANT", "some value");

whereas
define ("CONSTANT999", "some value");
 works fine.


25-Aug-2001 11:10

Major difference between PHP3 and PHP4:
If you define a function that uses some constant C before you
define() C, C will be undefined in the function in PHP3 but not
in PHP4. Thus constants are more pre-processor-ish in PHP3,
but probably more useful in PHP4.


21-Feb-2002 05:28

Also predefined (in PHP 4.1.1 on linux) are a set of LOG_* constants such
as LOG_DEBUG, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_CRIT.  I hit this when
writing an external logging facility, and couldn't work out why some of my
constants had the wrong values e.g.

<?php
define("LOG_DEBUG", 10);
print LOG_DEBUG;
?>

results in printing "7" rather than the "10" I had
expected.  This cost me about 15 minutes of grief.  I would have expected
some warning about trying to redefine a predefined constant.


25-Feb-2002 05:53

Warning, constants used within the heredoc syntax ()
are not interpreted!

Editor's Note: This is true. PHP has no way of recognizing the constant
from any other string of characters within the heredoc block.


23-Mar-2002 03:08

The __FILE__ constant in 4.2rc1 (CLI) will return the location of script
specified to be run, rather than the absolute file.

eg. /usr/bin/phpmole (a softlink to /usr/lib/php/phpmole/phpmole.php)

started like this
bash#/usr/bin/phpmole 
 
the line echo __FILE__ in phpmole.php will output /usr/bin/phpmole - in
the CGI it would have returned /usr/lib/php/phpmole/phpmole.php

the workaround is to check for links!!
$f = __FILE__;
if (is_link($f)) $f = readlink($f);

add a note about notes
previousVariables from outside PHPPredefined constantsnext
Last updated: Tue, 28 May 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Thu Jul 4 12:06:15 2002 CEST