PHP 8.5.2 Released!

define

(PHP 4, PHP 5, PHP 7, PHP 8)

defineDéfinit une constante

Description

define(string $constant_name, mixed $value, bool $case_insensitive = false): bool

Définit une constante à l'exécution.

Liste de paramètres

constant_name

Le nom de la constante.

Note:

Il est possible de définir avec define() des constantes avec des noms réservés ou même invalide, où leur valeur peuvent (seulement) être récupéré avec la fonction constant(). Cependant, faire ceci n'est pas recommandé.

value

La valeur de la constante.

Avertissement

Bien qu'il est techniquement possible de définir des constantes de type resource, ceci est déconseillé et peut causer des comportements inattendus.

case_insensitive

S'il vaut true, le nom de la constante sera insensible à la casse : CONSTANT et Constant représentent des valeurs identiques.

Avertissement

Définir des constantes insensibles à la casse est obsolète à partir de PHP 7.3.0. À partir de PHP 8.0.0, seul false est une valeur acceptable, passer true produira un avertissement.

Note:

Les constantes insensibles à la casse sont stockées en minuscule.

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Historique

Version Description
8.1.0 value peut désormais être un objet.
8.0.0 Passer true à case_insensitive émet désormais une E_WARNING. Passer false est toujours autorisé.
7.3.0 case_insensitive est obsolète et sera supprimée dans la version 8.0.0.

Exemples

Exemple #1 Définition d'une constante

<?php
define
("CONSTANT", "Bonjour le monde.");
echo
CONSTANT; // affiche "Bonjour le monde."
echo Constant; // affiche "Constant" et émet une alerte

define("GREETING", "Salut toi.", true);
echo
GREETING; // affiche "Salut toi."
echo Greeting; // affiche "Salut toi."

// Fonctionne depuis PHP 7
define('ANIMALS', array(
'chien',
'chat',
'oiseaux'
));
echo
ANIMALS[1]; // affiche "chat"

?>

Exemple #2 Constantes avec des Noms Réservés

Cet exemple illustre la possibilité de définir une constante avec le même nom qu'une constante magique. Puisque le comportement qui en résout porte à confusion, cette pratique n'est pas recommandée.

<?php
var_dump
(defined('__LINE__'));
var_dump(define('__LINE__', 'test'));
var_dump(constant('__LINE__'));
var_dump(__LINE__);
?>

L'exemple ci-dessus va afficher :

bool(false)
bool(true)
string(4) "test"
int(5)

Voir aussi

add a note

User Contributed Notes 4 notes

up
100
ravenswd at gmail dot com
10 years ago
Be aware that if "Notice"-level error reporting is turned off, then trying to use a constant as a variable will result in it being interpreted as a string, if it has not been defined.

I was working on a program which included a config file which contained:

<?php
define('ENABLE_UPLOADS', true);
?>

Since I wanted to remove the ability for uploads, I changed the file to read:

<?php
//define('ENABLE_UPLOADS', true);
?>

However, to my surprise, the program was still allowing uploads. Digging deeper into the code, I discovered this:

<?php
if ( ENABLE_UPLOADS ):
?>

Since 'ENABLE_UPLOADS' was not defined as a constant, PHP was interpreting its use as a string constant, which of course evaluates as True.
up
29
@SimoEast on Twitter
8 years ago
Not sure why the docs omit this, but when attempting to define() a constant that has already been defined, it will fail, trigger an E_NOTICE and the constant's value will remain as it was originally defined (with the new value ignored).

(Guess that's why they're called "constants".)
up
29
danbettles at yahoo dot co dot uk
16 years ago
define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

<?php
namespace test;
define('MESSAGE', 'Hello world!');
?>

The following code will define two constants in the "test" namespace.

<?php
namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>
up
4
eparkerii at carolina dot rr dot com
17 years ago
Found something interesting.  The following define:

<?php
define("THIS-IS-A-TEST","This is a test");
echo THIS-IS-A-TEST;
?>

Will return a '0'.

Whereas this:

<?php
define("THIS_IS_A_TEST","This is a test");
echo THIS_IS_A_TEST;
?>

Will return 'This is a test'.

This may be common knowledge but I only found out a few minutes ago.

[EDIT BY danbrown AT php DOT net: The original poster is referring to the hyphens versus underscores.  Hyphens do not work in defines or variables, which is expected behavior.]
To Top