define() function in PHP

The define() function defines a constant in PHP. Constants are global variables whose values cannot be changed during script execution.

Syntax

define(const_name, value, case_insensitive)

Parameters

  • const_name − The name of the constant (string).

  • value − The value of the constant (scalar value).

  • case_insensitive − Optional. Whether the constant name should be case-insensitive (default: false).

Return Value

The define() function returns true on success or false on failure.

Examples

Basic Example

The following example defines a constant and accesses it directly ?

<?php
    define("MESSAGE", "Hello, World!");
    echo MESSAGE;
?>
Hello, World!

Using constant() Function

You can also retrieve constant values using the constant() function ?

<?php
    define("PI", 3.14159);
    echo "Value of PI: " . constant("PI");
?>
Value of PI: 3.14159

Case-Insensitive Constants

This example creates a case-insensitive constant ?

<?php
    define("GREETING", "Good Morning!", true);
    echo greeting; // Works with lowercase
?>
Good Morning!

Conclusion

The define() function is essential for creating constants in PHP. Use it to define values that won't change throughout script execution, improving code maintainability and performance.

Updated on: 2026-03-15T07:35:53+05:30

930 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements