PHP.nl

defined

defined

Checks whether a constant with the given name exists

bool **defined** string $constant_name

Checks whether a constant with the given is defined. constant_name

This function works also with and . class constantsenum cases

Opmerking: > If you want to see if a variable exists, use as only applies to . If you want to see if a function exists, use . isset``definedconstantsfunction_exists

constant_nameThe constant name.

Returns true if the named constant given by has been defined, false otherwise. constant_name

Voorbeeld: Checking Constants

<?php

/* Note the use of quotes, this is important. This example is checking
 * if the string 'TEST' is the name of a constant named TEST */
if (defined('TEST')) {
    echo TEST;
}


interface bar {
    const test = 'foobar!';
}

class foo {
    const test = 'foobar!';
}

var_dump(defined('bar::test')); // bool(true)
var_dump(defined('foo::test')); // bool(true)

?>

Voorbeeld: Checking Enum Cases (as of PHP 8.1.0)

<?php

enum Suit
{
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}

var_dump(defined('Suit::Hearts')); // bool(true)

?>

define``constant``get_defined_constants``function_existsConstants