Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

defined

Checks whether a constant exists.

Syntax

defined(string $constant_name): bool

Parameters

ParameterTypeDescription
$constant_namestringThe constant name as a string.

Return value

bool`true` if the constant is defined.

Examples

A default value for a constant:

<?php
 
if (!defined('ITEMS_PER_PAGE')) {
    define('ITEMS_PER_PAGE', 20);
}
 
var_dump(defined('ITEMS_PER_PAGE'), defined('NO_SUCH_CONSTANT'));

Output:

bool(true)
bool(false)

Notes

Pass the name as a quoted string. Using a non-existent constant without the check raises an error.

See also

  • define — Creates a constant at run time.
  • constant — Returns the value of a constant by its name in a string.
  • isset — Checks that a variable exists and is not null.

In the tutorial

  • Constants — Constants in PHP — const and define, naming rules, built-in and magic constants.

Thanks for reading!

Donate to the site — we work hard for you.