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 8147543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:23:18+00:00 2026-06-06T14:23:18+00:00

What, exactly, is the purpose of using constants (in PHP)? I understand how they

  • 0

What, exactly, is the purpose of using constants (in PHP)? I understand how they work, but in which circumstances are they preferable over, say, a global variable? If anything, wouldn’t variables be more flexible because they can be placed inside strings?

I’ve searched the web, but all I’ve found is definitions–not actual reason for using one or the other. Can anyone help me to understand the benefits of using one over 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-06-06T14:23:19+00:00Added an answer on June 6, 2026 at 2:23 pm

    A constant is dependable. It will not change.
    Say for example You are writing a class library that others can use.
    Say you want to use Pi to 5 decimal places, and forwhatever reason you don’t want to make it a private variable with a getter.

    So you do

       Class Foo {
    
           public $pi = 3.14159;
       }
    

    Now some guy who is using your class accidently does

    $foo->pi = 4;
    

    Well that will screw everything up.
    So instead, you do

    const $pi = 3.14159;
    

    Now you know it won’t change and won’t screw stuff up. Plus developers will know that you have defined it that specific value for a reason.

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

Sidebar

Related Questions

Possible Duplicate: What exactly are hashtables? I understand the purpose of using hash functions
What exactly the main purpose of using RequestDispatcher , for example when it's executed
What exactly is the purpose of using malloc hooks? And I've read it's used
What, exactly, is the purpose of the intval() function for a base-10 calculation? Isn't
What exactly is the difference? I've been using Jetty 8.0.0 lately and when I
I'm trying to generate a simple static ELF using libelf, but I seem to
What is the main purpose of using CROSS APPLY ? I have read (vaguely,
in my test, i need to surf through site, using hyperlink, which name (and
{ _id : ObjectId(4c2209f9f3924d31102bd84a), name : mongo } what exactly is the purpose of
I have a system which is using Spring for dependency injection. I use annotation-based

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.