PHP Mastery Tutorial 0/120 lessons ~6 min read Lesson 8

    Constants

    Learn PHP constants — fixed values with define() and const, when to use them instead of variables, and best practices for configuration.

    Course progress0%
    Focus
    14 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Welcome to Constants

    Imagine you're building an e-commerce application.

    Some information changes frequently, such as:

    • Customer Name
    • Product Price
    • Order Status

    However, some values remain the same throughout the application.

    For example:

    • Company Name
    • Support Email
    • Tax Percentage
    • Website URL
    • Application Version

    These values rarely change while your application is running. Instead of storing them in variables, PHP provides a better solution called Constants.

    What is a Constant?

    A Constant is a named value that cannot be changed once it is created.

    Unlike variables, constants keep the same value throughout the execution of the program.

    Think of a constant as a permanent label.

    For example:

    • Company Name
    • Country Code
    • GST Rate
    • PI Value
    • Website URL

    These values usually remain fixed, making constants the perfect choice.

    Variable vs Constant

    Let's compare them.

    VariableConstant
    Value can changeValue cannot change
    Starts with $No $ symbol
    Used for dynamic dataUsed for fixed data
    Created using assignment (=)Created using define() or const
    Example: Customer NameExample: Company Name

    Rule of Thumb: If the value is expected to change, use a variable. If the value should always remain the same, use a constant.

    Creating a Constant

    PHP provides two ways to create constants.

    Using define()

    php
    <?php
    define("COMPANY_NAME", "TechLearningPro");
    echo COMPANY_NAME;
    ?>

    Output

    TechLearningPro

    The first argument is the constant name, and the second is its value.

    Using const

    php
    <?php
    const WEBSITE = "https://techlearningpro.com";
    echo WEBSITE;
    ?>

    Both approaches create constants. You'll commonly see const used inside classes and define() in configuration files.

    Real-World Example

    Imagine you're developing an online shopping application.

    Some information never changes.

    php
    <?php
    define("COMPANY_NAME", "TechLearningPro");
    define("SUPPORT_EMAIL", "support@techlearningpro.com");
    define("GST_RATE", 18);
    echo COMPANY_NAME;
    echo "<br>";
    echo SUPPORT_EMAIL;
    echo "<br>";
    echo GST_RATE . "%";
    ?>

    Output

    TechLearningPro
    
    support@techlearningpro.com
    
    18%

    If these values need to change in the future, you update them in one place instead of searching through the entire application.

    Can a Constant Be Changed?

    No. Once a constant is created, its value cannot be modified.

    php
    <?php
    define("PI", 3.14159);
    PI = 3.14; // Not Allowed
    ?>

    PHP will generate an error because constants are read-only. This protects important application settings from accidental changes.

    When Should You Use Constants?

    Use constants whenever the value should remain fixed.

    Examples include:

    • Company Name
    • Website URL
    • Database Version
    • Tax Percentage
    • Currency Code
    • API Version
    • Application Environment

    Avoid using constants for user-specific information such as names, email addresses, or shopping cart totals.

    Best Practices

    • Use constants only for values that never change.
    • Write constant names in UPPERCASE for better readability.
    • Use meaningful names like COMPANY_NAME or MAX_UPLOAD_SIZE.
    • Store application configuration values as constants.
    • Group related constants together in configuration files.

    Common Mistakes

    Using a variable instead of a constant

    php
    $companyName = "TechLearningPro";

    Better

    php
    define("COMPANY_NAME", "TechLearningPro");

    Trying to modify a constant

    php
    define("GST", 18);
    GST = 20;

    A constant cannot be reassigned after it is created.

    Using unclear constant names

    php
    define("A", 100);

    Better

    php
    define("MAX_LOGIN_ATTEMPTS", 5);

    Meaningful names make your code easier to understand.

    Hands-on Exercise

    Task: Create constants for Company Name, Website URL, Support Email, and GST Rate. Display all four values on the screen.

    Challenge: Imagine you're building a movie ticket booking application. Create constants for CINEMA_NAME, TICKET_BOOKING_FEE, and CUSTOMER_SUPPORT_NUMBER. Print all the values in a formatted output.

    php
    <?php
    // TODO: Task
    define("COMPANY_NAME", "TechLearningPro");
    // ...
    // TODO: Challenge — movie booking constants
    ?>

    Summary

    • Constants store values that never change during program execution.
    • They help keep important configuration values safe and organized.
    • Constants do not use the $ symbol.
    • You can create constants using define() or const.
    • Once created, a constant cannot be modified.

    Key Takeaways

    • Use variables for changing values.
    • Use constants for fixed values.
    • Constants improve readability and maintainability.
    • Name constants using uppercase letters.
    • Protect important application settings with constants.

    Interview Questions

    Beginner

    Q1What is a constant in PHP?
    A named value that cannot be changed once defined. Constants hold fixed configuration or application settings throughout script execution.
    Q2How is a constant different from a variable?
    Variables use $ and can be reassigned; constants have no $, are defined with define() or const, and cannot be modified after creation.
    Q3Which symbol is not used with constants?
    The dollar sign ($). Constants are referenced by name only, e.g. echo COMPANY_NAME; not $COMPANY_NAME.
    Q4How do you create a constant using define()?
    define("CONSTANT_NAME", value); — first argument is the name (string), second is the value. Example: define("GST_RATE", 18);
    Q5Can a constant's value be changed?
    No. Attempting to reassign a constant causes a fatal error. Constants are read-only by design.

    Intermediate

    Q6What is the difference between define() and const?
    define() works anywhere at runtime and accepts dynamic names. const is compile-time, used at top level or inside classes, and requires a fixed identifier.
    Q7Why are constants commonly used for configuration values?
    They centralize settings (API URLs, tax rates, app name), prevent accidental overwrites, and make updates easy — change one definition instead of hunting through code.
    Q8When should you choose a constant over a variable?
    When the value must never change during execution — company name, max upload size, environment flags — not for user input or session data.

    Advanced

    Q9How are class constants different from global constants?
    Class constants (public const STATUS = 'active') belong to the class, accessed via ClassName::STATUS. Global constants from define()/const live in the global namespace.
    Q10How do constants improve the maintainability of large PHP applications?
    Configuration lives in one place, magic numbers disappear, renaming is safer, and teams can grep constant names to find all usages across a codebase.

    What's Next?

    Now that you know how to store fixed values using constants, it's time to learn how PHP performs calculations and comparisons.

    In the next lesson, Operators, you'll use arithmetic, comparison, and logical operators to work with variables and constants in real application logic.

    Ready to mark this lesson complete?Track your journey across the entire course.