PHP define() Function: Syntax, Examples & Usage

1. Introduction

The define() function is used to create global constants in PHP. Once a constant is defined, its value cannot be changed or undefined later in the script.

📖
Important Note:
  • Constants cannot change their value once defined.
  • Constants do not use the dollar sign ($).
  • Constants are usually written in uppercase letters by convention.
  • Constants can be accessed from anywhere in the script if they are global.

Constants are commonly used for:

  1. System settings
  2. Application limits
  3. Fixed values such as website name or version number

Using constants makes programs more secure and easier to maintain.

2. Syntax of define() Function

The basic syntax of the define() function is:


bool define ( string $name , mixed $value [, bool $case_insensitive = false ] )

Parameters Explanation

ParameterDescription
$nameThe name of the constant. This parameter is required.
$valueThe value assigned to the constant. This parameter is required.
$case_insensitiveOptional parameter. If set to true, the constant becomes case-insensitive. Default value is false.

3. Important Rules of define()

There are some important rules when working with the define() function.

1. Cannot Be Used Inside Class

Using define() inside a class does not work properly for class constants. Instead, the const keyword should be used for class constants.

2. Value Cannot Be Changed

Once a constant is defined, its value cannot be modified later.

Example:


define("SITE_NAME", "TechFliez");

After defining it, you cannot change the value.

3. No Dollar Sign

Unlike variables, constants do not use $ symbol.

Example:

Correct:


define("SITE_NAME", "TechFliez");
echo SITE_NAME;

Incorrect:


echo $SITE_NAME;

4. Constant Values

Constant values can be:

  • Numbers
  • Strings
  • Boolean values

Example:


define("MAX_USERS", 100);
define("SITE_NAME", "TechFliez");

4. Example of PHP define()

Let us look at a simple example of defining a constant using define().

Example


define("IMAGE_WIDTH", "400px");

echo IMAGE_WIDTH;

Output

400px

Explanation

In this example:
1. We defined a constant named IMAGE_WIDTH.

2. The value assigned is 400px.

3. The constant is printed using:


echo IMAGE_WIDTH;

Since constants do not use $, we directly use the constant name.

5. Case Sensitivity in define()

By default, constants created using define() are case-sensitive.
Example


define("IMAGE_WIDTH", "400px");
echo IMAGE_WIDTH;
echo IMAGE_width;

Output

400px
Notice: Use of undefined constant IMAGE_width

Explanation

Here:

  • IMAGE_WIDTH works correctly.
  • IMAGE_width produces an error because the constant name is case-sensitive.

6. Case-Insensitive Constant

The third parameter of define() allows constants to be case-insensitive.

Example


define("IMAGE_WIDTH", "400px", true);

echo IMAGE_WIDTH;
echo IMAGE_width;

Output

400px 400px

Explanation

In this example:


define("IMAGE_WIDTH", "400px", true);

The third parameter true makes the constant case-insensitive.

So both of these work:


echo IMAGE_WIDTH;
echo IMAGE_width;

However, case-insensitive constants are deprecated in modern PHP versions, so developers usually avoid using this feature.

7. Real-World Example

Let us see a practical example where constants are used in a website configuration system.

Example


define("SITE_NAME", "TechFliez");
define("SITE_URL", "https://www.techfliez.com");
define("ADMIN_EMAIL", "admin@techfliez.com");

echo "Website Name: " . SITE_NAME;
echo "
"; echo "Website URL: " . SITE_URL; echo "
"; echo "Admin Email: " . ADMIN_EMAIL;

Output

Website Name: TechFliez Website URL: https://www.techfliez.com Admin Email: admin@techfliez.com

Explanation

In this example:

  • We defined three global constants:
    • SITE_NAME
    • SITE_URL
    • ADMIN_EMAIL

These constants store fixed configuration values.

Such constants are commonly used in:

  • Website configuration files
  • Application settings
  • Database configuration
  • System limits

8. Advantages of Using define()

Using the define() function offers several benefits.

1. Global Access

Constants defined using define() are globally accessible throughout the script.

2. Data Security

Since constants cannot change, important values remain protected.

3. Code Readability

Constants improve code readability by using meaningful names.

Example:


MAX_UPLOAD_SIZE
SITE_NAME
DATABASE_VERSION

These names clearly describe their purpose.

4. Easy Maintenance

If a value needs to be updated, it can be changed in one place only.

9. Difference Between define() and const

Feature define() const
Used For Global constants Class constants
Location Outside class Inside class
Syntax define(“NAME”, value) const NAME = value
Runtime Defined at runtime Defined at compile time

Example:


define("SITE_NAME", "TechFliez");

vs


class Config {
   const SITE_NAME = "TechFliez";
}

11. Conclusion

The define() function in PHP is used to create global constants, which store fixed values that remain unchanged during the execution of a program.

Constants defined with define() do not use the $ symbol and can be accessed from anywhere in the script. Once defined, their values cannot be modified, which makes them ideal for storing configuration values, system settings, and application constants.

Although define() allows optional case-insensitive constants, modern PHP versions recommend using case-sensitive constants for better performance and compatibility.

Define keyword – Interview Questions

Q 1: What is define() in PHP?
Ans: A function used to define constants.
Q 2: Can define() be used inside a class?
Ans: Yes.
Q 3: Is define() evaluated at runtime?
Ans: Yes.
Q 4: Can constants created by define() be global?
Ans: Yes.
Q 5: Difference between define() and const?
Ans: define() is runtime; const is compile-time.

Define keyword – Objective Questions (MCQs)

Q1. Which function defines a global constant?






Q2. Constants created with define() are ______.






Q3. define("PI", 3.14); creates a constant accessible ______.






Q4. Can define() create constants at runtime?






Q5. Accessing a constant created by define() uses ______.






Related PHP Tutorials