PHP define vs const

0 votes

In PHP, there are two ways to declare constants :

  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 then use the other?
Jun 1, 2022 in PHP by Kichu
• 19,040 points
1,040 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Const creates constants at compile-time, whereas define defines them at run time, which is the key distinction between the two methods. The majority of const's drawbacks stem from this.

Some disadvantages of const are:

  •  Cannot be used to conditionally define constants. 
  • Unlike define(), which accepts any expression, const accepts a static scalar (an integer, string, or other constant like true, false, null, or __FILE__). Constant expressions are now permitted in const since PHP 5.6.
  • Unlike define(), which accepts any expression as a name, const requires a simple constant name.
I hope this helps you.

answered Jun 28, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
0 answers

php var_dump() vs print_r()

What is the difference between var_dump() and print_r() in terms of ...READ MORE

May 28, 2022 in PHP by Kichu
• 19,040 points
603 views
0 votes
0 answers

PHP "php://input" vs $_POST

I want to know what is the ...READ MORE

Jun 9, 2022 in PHP by Kichu
• 19,040 points
731 views
0 votes
0 answers

PHP Elvis operator vs null coalescing operator

Can someone clarify the differences between PHP's ...READ MORE

Aug 3, 2022 in PHP by Kithuzzz
• 38,000 points
809 views
0 votes
0 answers

How to echo define value in PHP?

 Code : <?php define("SITENAME","Page TITLE");?> I want to echo ...READ MORE

Aug 6, 2022 in PHP by Kithuzzz
• 38,000 points
573 views
0 votes
0 answers

getenv() vs. $_ENV in PHP

What is the distinction between $_ENV and ...READ MORE

Aug 8, 2022 in PHP by narikkadan
• 63,600 points
1,882 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
11,024 views
0 votes
0 answers

How to check if a defined constant exists in PHP?

I am using fuelphp a PHP framework and I ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,040 points
1,109 views
0 votes
0 answers

How to add elements to an empty array in PHP?

I define an array in PHP : $cart ...READ MORE

Jun 3, 2022 in PHP by Kichu
• 19,040 points
1,036 views
0 votes
0 answers

PHP - If variable is not empty, echo some html code

I want to display some HTML code ...READ MORE

Jun 4, 2022 in PHP by Kichu
• 19,040 points
1,006 views
0 votes
0 answers

Static variables in PHP

Can someone please explain to me what ...READ MORE

Jun 12, 2022 in PHP by narikkadan
• 63,600 points
577 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP