Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

define()

Call define to create a case-sensitive named constant at runtime; the name is supplied as a string and may include a namespace.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Call define to create a case-sensitive named constant at runtime; the name is supplied as a string and may include a namespace. A successful definition returns true. Redefining the same name emits E_WARNING, returns false and leaves the original value intact. With a fresh name, case_insensitive=true emits E_WARNING but returns true and creates a case-sensitive constant.

Signature

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

Example output

[true,3,false]
warning [false,3]
warning [true,true,false]

Examples

<?php
$key = "Example\\LEVEL";
echo json_encode([define($key, 3), constant($key), defined("Example\\level")]), PHP_EOL;
set_error_handler(function (int $severity): bool {
    echo $severity === E_WARNING ? "warning " : "other ";
    return true;
});
try {
    $ok = define($key, 9);
    echo json_encode([$ok, constant($key)]), PHP_EOL;
    $fresh = define("FreshKey", 7, true);
    echo json_encode([$fresh, defined("FreshKey"), defined("freshkey")]);
} finally { restore_error_handler(); }

Result

[true,3,false]
warning [false,3]
warning [true,true,false]

Sources