Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 895749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:36:34+00:00 2026-05-15T14:36:34+00:00

In PHP, you can declare constants in two ways: With define keyword define(‘FOO’, 1);

  • 0

In PHP, you can declare constants in two ways:

  1. With define keyword

    define('FOO', 1);
    
  2. Using const keyword

    const FOO = 1;
    

  • What are the main differences between those two?
  • When and why should you use one and when use the other?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:36:35+00:00Added an answer on May 15, 2026 at 2:36 pm

    As of PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() function:

    const FOO = 'BAR';
    define('FOO', 'BAR');
    

    The fundamental difference between those two ways is that const defines constants at compile time, whereas define defines them at run time. This causes most of const‘s disadvantages. Some disadvantages of const are:

    • const cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:

       if (...) {
           const FOO = 'BAR';    // Invalid
       }
       // but
       if (...) {
           define('FOO', 'BAR'); // Valid
       }
      

      Why would you want to do that anyway? One common application is to check whether the constant is already defined:

       if (!defined('FOO')) {
           define('FOO', 'BAR');
       }
      
    • const accepts a static scalar (number, string or other constant like true, false, null, __FILE__), whereas define() takes any expression. Since PHP 5.6 constant expressions are allowed in const as well:

       const BIT_5 = 1 << 5;    // Valid since PHP 5.6 and invalid previously
       define('BIT_5', 1 << 5); // Always valid
      
    • const takes a plain constant name, whereas define() accepts any expression as name. This allows to do things like this:

       for ($i = 0; $i < 32; ++$i) {
           define('BIT_' . $i, 1 << $i);
       }
      
    • consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument (Note: defining case-insensitive constants is deprecated as of PHP 7.3.0 and removed since PHP 8.0.0):

       define('FOO', 'BAR', true);
       echo FOO; // BAR
       echo foo; // BAR
      

    So, that was the bad side of things. Now let’s look at the reason why I personally always use const unless one of the above situations occurs:

    • const simply reads nicer. It’s a language construct instead of a function and also is consistent with how you define constants in classes.

    • const, being a language construct, can be statically analysed by automated tooling.

    • const defines a constant in the current namespace, while define() has to be passed the full namespace name:

       namespace A\B\C;
       // To define the constant A\B\C\FOO:
       const FOO = 'BAR';
       define('A\B\C\FOO', 'BAR');
      
    • Since PHP 5.6 const constants can also be arrays, while define() does not support arrays yet. However, arrays will be supported for both cases in PHP 7.

       const FOO = [1, 2, 3];    // Valid in PHP 5.6
       define('FOO', [1, 2, 3]); // Invalid in PHP 5.6 and valid in PHP 7.0
      

    Finally, note that const can also be used within a class or interface to define a class constant or interface constant. define cannot be used for this purpose:

    class Foo {
        const BAR = 2; // Valid
    }
    // But
    class Baz {
        define('QUX', 2); // Invalid
    }
    

    Summary

    Unless you need any type of conditional or expressional definition, use consts instead of define()s – simply for the sake of readability!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What strikes me is that you do not take the… May 16, 2026 at 12:01 pm
  • Editorial Team
    Editorial Team added an answer jeweler was created before RubyGems became what it is, so… May 16, 2026 at 12:01 pm
  • Editorial Team
    Editorial Team added an answer Yes, that will work. Incidentally, the Boolean keywords are true… May 16, 2026 at 12:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In PHP 5 I can declare a const value to a class: class config
in php you can declare a function in a global scope with function. is
Using PHP can you create a pseudo form submit without ever generating a form?
In PHP, I can write: $vname = 'phone'; $$vname = '555-1234'; print $phone; ...
http://forumgallery.rollinleonard.com/artists.php I can't seem to get rid of the space before the comma in
Let's imagine that we have something like: $.post('somescript.php', 'WHAT CAN WE PUT HERE?', function(replyData)
How we can integrate PHP in Google Calender API without Zend?while i google it
Is there any PHP application that can automatically generate add/edit forms and listing pages
How can I dynamically pass items to class function? For example here it is
I'm writing a simple templating layer in PHP but I've got myself a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.