|
|
Une constante est un identifiant (un nom) qui repr�sente une valeur
simple. Comme leur nom le sugg�re, cette valeur ne peut jamais �tre
modifi�e durant l'ex�cution du script (les constantes magiques
__FILE__ et
__LINE__ sont les seules exception). Le nom d'une constante
est sensible � la casse, par d�faut. Par convention, les constantes sont
toujours en majuscules.
Les noms de constantes suivent les m�mes r�gles que n'importe
quel nom en PHP. Un nom de constante valide commence par une
lettre ou un soulign� (_), suivi d'un nombre
quelconque de lettre, chiffres ou soulign�s. Sous forme d'expression
r�guli�re, cela peut s'exprimer comme ceci :
[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
Note�:
Dans cette documentation, une lettre peut �tre un des caract�res
suivants : de a � z, de A � Z et tous les caract�res ASCII
de 127 � 255 (0x7f-0xff).
Les constantes sont accessibles de mani�re globale.
Vous pouvez d�finir une constante en utilisant la fonction
define(). Une fois qu'une constante est d�finie,
elle ne peut jamais �tre modifi�e, ou d�truite.
Seuls les types de donn�es scalaires peuvent �tre plac�s dans une
constante : c'est � dire les types bool�en, entier, double et
cha�ne de caract�res (soit boolean, integer,
double et string).
Vous povuez acc�der � la valeur d'une constante en sp�cifiant simplement
son nom. Contrairement aux variables, vous ne devez PAS pr�fixer
le nom de la constante avec $.
Vous pouvez aussi utiliser la fonction constant(), pour
lire dynamiquement la valeur d'une constante, si vous obtenez le nom
de cette constante dynamiquement (retour de fonction, par exemple).
Utilisez la fonction get_defined_constants() pour conna�tre
la liste de toutes les fonctions d�finies.
Note�:
Les constantes et les variables globales utilisent deux espaces de
noms diff�rents. Ce qui implique que TRUE et
$TRUE sont g�n�ralement diff�rents (en tous cas, ils
peuvent avoir des valeurs diff�rentes).
Lorsque vous utilisez une constante non d�finie, PHP suppose que vous utilisez
le nom de la constante. Une
note sera g�n�r�e. Utilisez la
fonction defined() pour savoir si une constante existe ou pas.
Il y a des diff�rences entre les constantes et les variables :
Les constantes ne commencent pas par le signe ($);
Les constantes sont d�finies et accessibles � tout endroit du code,
globalement.
Les constantes ne peuvent pas �tre red�finies ou ind�finies une fois qu'elles
ont �t� d�finies.
Les constantes ne peuvent contenir que des scalaires.
Exemple 8-1. Definir une constante <?php
define("CONSTANTE", "Bonjour le monde.");
echo CONSTANTE; // affiche "Bonjour le monde."
echo Constante; // affiche "Constante" et une note.
?> |
|
User Contributed Notes Les constantes |
|
[email protected]
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.
|
|
[email protected]
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.
|
|
[email protected]
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.
|
|
[email protected]
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)
|
|
[email protected]
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>
|
|
[email protected]
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.
|
|
[email protected]
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.
|
|
[email protected]
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.
|
|
[email protected]
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.
|
|
[email protected]
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.
|
|
[email protected]
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);
|
|
[email protected]
18-Jul-2002 07: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
|
|
|
| |