In PHP, how do you define a constant value that cannot be changed at runtime?

Difficulty: Easy

Correct Answer: Use the define() function or the const keyword, for example define("SITE_NAME", "MySite") or const SITE_NAME = "MySite"; to create a constant that cannot be reassigned at runtime.

Explanation:


Introduction / Context:
Constants in PHP store values that should not change during the execution of a script. They are often used for configuration values, version numbers, or other fixed settings. This question asks how to declare such constants correctly, as opposed to using normal variables that can be reassigned.


Given Data / Assumptions:

    • We want a value that remains the same for the entire script execution.
    • The value should be accessible without using a dollar symbol prefix.
    • The language in question is PHP, which provides both define() and const.


Concept / Approach:
PHP offers two main ways to define constants. The define() function accepts a constant name and a value, and creates a constant that can be used globally. For example define("PI", 3.14); defines a constant PI. The const keyword can also be used at the top level or inside classes, as in const VERSION = "1.0";. These constants do not use the dollar symbol when referenced and cannot be reassigned once defined during normal execution. This behaviour distinguishes them from regular variables such as $pi, which can be changed freely.


Step-by-Step Solution:
Step 1: Recall that constants in PHP are created with either define() or const.Step 2: Recognise that define() is a function call, typically used like define("SITE_NAME", "MySite").Step 3: Recognise that const is a language construct used as const SITE_NAME = "MySite"; especially at the top of files or inside class definitions.Step 4: Note that constants are used without a dollar symbol, for example echo SITE_NAME;.Step 5: Option A correctly describes these mechanisms and provides clear examples, so it is the correct answer.


Verification / Alternative check:
Testing the code define("DEMO", "X"); echo DEMO; shows that the constant can be used like a global fixed value. Trying to assign DEMO = "Y"; will cause an error. Similarly, const SAMPLE = 10; works at the top level in supported PHP versions. This confirms that define() and const create constants that cannot be changed in the same way as normal variables.


Why Other Options Are Wrong:
Option B relies only on convention and does not provide true immutability, so it does not define a constant in language terms. Option C claims that surrounding a value with single quotes in a string automatically creates a constant, which is not true. Option D confuses static variables with constants; static affects storage and visibility but does not prevent reassignment.


Common Pitfalls:
One pitfall is forgetting that constant names are case sensitive by default when using define(), unless a third parameter is used in older PHP versions. Another is mixing up variable and constant naming conventions, which can lead to confusion. Many teams adopt naming conventions such as all caps for constants to make their role clear in the codebase.


Final Answer:
Use the define() function or the const keyword, for example define("SITE_NAME", "MySite") or const SITE_NAME = "MySite"; to create a constant that cannot be reassigned at runtime.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion