update page now
PHP 8.5.2 Released!

define

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

defineDefiniert eine benannte Konstante

Beschreibung

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

Definiert während der Laufzeit eine benannte Konstante.

Parameter-Liste

constant_name

Der Name der Konstante.

Hinweis:

Es ist möglich, Konstanten mit reservierten oder gar ungültigen Namen mittels define() zu definieren, deren Wert (nur) mittels constant() gelesen werden kann. Allerdings wird dies nicht empfohlen.

value

Der Wert der Konstante.

Warnung

Obgleich es möglich ist, Konstanten vom Typ Ressource zu definieren, wird dies nicht empfohlen, da es ein unvorhersagbares Verhalten des Programms zur Folge haben kann.

case_insensitive

Falls auf true gesetzt, wird bei der Konstante nicht zwischen Groß- und Kleinschreibung unterschieden. In der Voreinstellung wird dazwischen unterschieden, d. h. KONSTANTE und Konstante repräsentieren unterschiedliche Werte.

Warnung

Das Definieren von Konstanten, die nicht zwischen Groß- und Kleinschreibung unterscheiden, wird von PHP 7.3.0 an missbilligt. Seit PHP 8.0.0 ist nur false ein zulässiger Wert; die Übergabe von true erzeugt eine Warnung.

Hinweis:

Groß-/Kleinschreibungsunabhängige Konstanten werden kleingeschrieben gespeichert.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Changelog

Version Beschreibung
8.1.0 value kann nun ein Objekt sein.
8.0.0 Die Übergabe von true an case_insensitive gibt nun einen Fehler der Stufe E_WARNING aus; die Übergabe von false ist weiterhin erlaubt.
7.3.0 case_insensitive ist missbilligt und wird in Version 8.0.0 entfernt.

Beispiele

Beispiel #1 Konstanten definieren

<?php
define
("KONSTANTE", "Hallo Welt.");
echo
KONSTANTE; // gibt "Hallo Welt." aus
echo Konstante; // gibt "Konstante" aus und erzeugt eine Warnung

define("BEGRUESSUNG", "Hallo Du.", true);
echo
BEGRUESSUNG; // gibt "Hallo Du." aus
echo Begruessung; // gibt "Hallo Du." aus

// Funktioniert ab PHP 7
define('ANIMALS', array(
'Hund',
'Katze',
'Maus'
));
echo
ANIMALS[1]; // gibt "Katze" aus

?>

Beispiel #2 Konstanten mit reservierten Namen

Dieses Beispiel verdeutlich die Möglichkeit, eine Konstante mit dem selben Namen wie eine magische Konstante zu definieren. Da das resultierende Verhalten offensichtlich verwirrend ist, ist es nicht empfehlenswert, dies in der Praxis zu tun.

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch

  • defined() - Prüft, ob eine Konstante mit dem angegebenen Namen existiert
  • constant() - Liefert den Wert einer Konstante
  • Das Kapitel über Konstanten

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