PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<Variables from outside PHPExpressies>
view the version of this page
Last updated: Tue, 22 Apr 2003

Hoofdstuk 9. Constanten

PHP defineert aan aantal constanten en biedt een mechanisme om zelf constanten te defineren in de code. Constanten lijken erg op variabelen, behalve dat constanten moeten worden gedefineerd met de define() functie, en dat ze later niet meer kunnen worden veranderd.

De altijd aanwezige voorgedefineerde constanten zijn:

__FILE__

De naam van het script dat op het moment wordt uitgevoerd. Als het wordt gebruikt binnen een ge-include of ge-required bestand, dan wordt de naam van het included bestand gegeven, en niet de naam van het parent bestand.

__LINE__

Het regelnummer binnen het script dat op het moment wordt uitgevoerd. Als het wordt gebruikt binnen een ge-include of ge-required bestand, dan wordt de positie binnen het included bestand gegeven.

PHP_VERSION

De string die de versie aangeeft van de in gebruik zijnde PHP parser; bijvoorbeeld '4.0.4-dev'.

PHP_OS

De naam van het besturingssysteem waarop de PHP parser draait; bijvoorbeeld 'Linux'.

TRUE

Een ware waarde.

FALSE

Een niet-ware waarde.

E_ERROR

Geeft een fout aan, anders dan een parse fout, waarop geen actie kan worden ondernomen.

E_WARNING

Geeft een conditie aan waarin PHP weet dat er iets verkeerd is, maar toch doorgaat; dit type fouten kunnen door het script zelf worden opgevangen. Een voorbeeld zou een ongeldige regexp in ereg() kunnen zijn.

E_PARSE

De parser heeft een fatale fout in het script gevonden. Het is niet mogelijk door te gaan.

E_NOTICE

Soms gebeurt er iets wat een fout kan zijn of niet. Het script wordt wel verder uitgevoerd. Een voorbeeld is het gebruik van bijvoorbeeld $foo[bar], of het gebruiken van een nog niet bestaande variabele.

E_ALL

Alle E_* constanten in ��n. Indien dit samen wordt gebruikt met error_reporting() worden alle fouten en problemen gerapporteerd door PHP.

De E_* constanten worden normaal gebruikt met de error_reporting() functie om het fout rapporteer niveau te zetten. Zie voor al deze constanten: Error handling.

Ju kunt zelf constanten toevoegen met de define() functie.

Let er op dat dit constanten zijn, en geen C-achtige macros; alleen geldige scalaire gegevens mogen wordt gebruikt voor constanten.

Voorbeeld 9-1. Constanten defineren

<?php
define("CONSTANT", "Hallo wereld.");
echo CONSTANT; // print "Hallo wereld."
?>

Voorbeeld 9-2. Het gebruik van __FILE__ en __LINE__

<?php
function report_error($bestand, $regel, $tekst) {
    echo "Er is een fout opgetreden in $bestand op regel $regel: $tekst.";
}

report_error(__FILE__,__LINE__, "D'r ging iets fout!");
?>



User Contributed Notes
Constanten
add a note add a note
tom dot harris at home dot com
05-Aug-2000 12: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.

silvein at sonique dot com
24-Jan-2001 12: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.
yasuo_ohgaki at hotmial dot com
09-Feb-2001 02: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)

pierre dot delandsheere at rug dot ac dot be dot NOSPAM
31-Mar-2001 05: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>

afuse at yahoo dot com
11-Jun-2001 05: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.

jromalo at hotmail dot com
22-Jun-2001 06: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.

carl at NOSPAM dot thep dot lu dot se
26-Aug-2001 12: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.

tlittle at deepdesign dot com dot au
21-Feb-2002 06: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.

katana at katana-inc dot com
25-Feb-2002 06: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.

alan at akbkhome dot com
23-Mar-2002 04: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);

sean at php dot net
18-Jul-2002 08:27

__FILE__ doesn't work with Zend Encoder 2.0.x (ZE does funky things with __FILE__).

This may  be fixed in the future, but we don't know when. In the mean time, Zend has suggested we use the undocumented "zend_loader_current_file()", which seems to do the same thing as __FILE__ for me.

S

fmmarzoa at gmx dot net
09-Oct-2002 05:38

Constants can be useful for i18n and l10n (internationalization and localization). I'm using a module for each language my application supports for string isolation. Each module only contains constants definitions. In example, I've this in my module 'en.php' (English):

define (strSayHello, 'Hello!');

While in my module 'es.php' (Spanish), I've the constant redefined:

define (strSayHello, '�Hola!');

The language module needed is dinamically choosen by the application based on user preferences.

All works ok, but I've had a trouble with constants that must include variable values, in example, I've something like:

define (strArticleDescr, 'Published by $article_author on $article_date in $article_lang_name');

Now, when I try to use this, the value is interpreted literally, so:

print strArticleDescr;

Will show:

Published by $article_author on $article_date in $article_lang_name

I've found a solution for this using 'eval' for parsing the constant, thats:

eval ('print "'.strArticleDescr.'";')

And now things works ok, like in example:

Published by Francisco M. Marzoa on 2002-10-09 15:39:10 in Spanish

Hope you find this useful.

gv (at) damnsw (dot) net
06-Nov-2002 03:08

fmmarzoa: In PHP 4.2.2/CLI, I had no problem setting define()'s to the contents of variables:

<?
$foo = "PHP";
define( "bar", "$foo is a good thing." );
print bar;
?>

Will print "PHP is a good thing.".

A notable difference, however, between my example and yours is your use of single-quotes.  Strings in single quotes (') will not be expanded:

print '$foo';

Will print '$foo', not the contents of $foo.



--gv

hoser at yahoo dot com
12-Nov-2002 10:58

where did all the comments go?

This is really not an appropriate comment -- I hate to be a t r o l l, please delete this.

I can't edit this comment

alexatcite.gda.itesm.mx
26-Nov-2002 10:01

I got some weird behavior when initializing variables with constants.

For some reason the following code works perfectly:
$status |= STAGEONE;
$status &= EMPTY;
where both constants are integers.

But I can't seem to do this:
$status = EMPTY;

I found a way around it:
$status = constant ('EMPTY');
but I wonder why this is so...

Mike Powell
25-Mar-2003 03:46

In response to the notes above about variable references in constants, double quotes isn't a proper solution because it parses the variable at the time the constant is defined. The desired behavior is to have the variables parsed at the time the constant is referenced, and this behavior can really only be achieved by using eval(), as described above.
add a note add a note

<Variables from outside PHPExpressies>
 Last updated: Tue, 22 Apr 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Sat May 24 21:09:36 2003 CEST