Content · Programming · PHP

PHP defined() Function: Usage and Examples

Learn how PHP defined works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Checks whether a constant has been defined.

What Is defined() in PHP?

Checks whether a constant has been defined.

Its syntax is:

defined(string $constant_name): bool

Parameters:

  • $constant_name — Accepted as string; required argument.

Return type: bool. Use defined() to test constants, not isset(), which checks variables and array elements.

Availability: Core PHP.

Example 1: Basic Usage

<?php

var_dump(defined("PHP_VERSION"));

Output:

bool(true)

Use defined() to test constants, not isset(), which checks variables and array elements.

Example 2: A Different Input

<?php

var_dump(defined("NOT_A_REAL_CONSTANT"));

Output:

bool(false)

Use defined() to test constants, not isset(), which checks variables and array elements.

Practical Example: Applying It in a Program

<?php

$constantNames = ["PHP_VERSION", "PHP_INT_MAX"]; foreach ($constantNames as $constantName) { if (defined($constantName)) { echo constant($constantName) . PHP_EOL; } }

Output:

8.4.24
9223372036854775807

Use defined() to test constants, not isset(), which checks variables and array elements.

Important Behavior and Edge Cases

  • Key behavior: Use defined() to test constants, not isset(), which checks variables and array elements.
  • Environment: Core PHP.

Comparison With Related Functions

FunctionPurpose
defined()Checks whether a constant has been defined.
constant()constant() reads a named constant; defined() only checks for existence.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Use defined() to test constants, not isset(), which checks variables and array elementsUse defined() to test constants, not isset(), which checks variables and array elements.

Key Takeaways

  • Checks whether a constant has been defined.
  • Use defined() to test constants, not isset(), which checks variables and array elements.
  • Core PHP.

Summary: Use defined() to test constants, not isset(), which checks variables and array elements.

Share this resourceSend this page to someone who may find it useful.