66

Can someone explain the difference between using

define('SOMETHING', true);

and

$SOMETHING = true;

And maybe the benefits between one or the other?

I use variables everywhere and even in a config type file that is included to everypage I still use variables as I don't see why to use the define method.

0

5 Answers 5

93

DEFINE makes a constant, and constants are global and can be used anywhere. They also cannot be redefined, which variables can be.

I normally use DEFINE for Configs because no one can mess with it after the fact, and I can check it anywhere without global-ling, making for easier checks.

1
  • Can I pass the user's id from database into session? e.g. define($_SESSION['user_id'],row['id']) which the row['id'] is coming from database Jan 3, 2017 at 8:13
14

Once defined, a 'constant' cannot be changed at runtime, whereas an ordinary variable assignment can.

Constants are better for things like configuration directives which should not be changed during execution. Furthermore, code is easier to read (and maintain & handover) if values which are meant to be constant are explicitly made so.

9

There is also a difference in scope.

In the example given by the orignal poster, $SOMETHING will not be accessible within a function whereas define('SOMETHING', true) will be.

1
  • that was a great and useful tip in config files with lots of functions inside it
    – Saghachi
    Feb 10, 2023 at 12:21
7

define() makes a read-only variable, compared to a standard variable that supports read and write operations.

4
  • 1
    just a note - i don't see how the term "read-only variables" applies to define() - as the manual states, it defines a named constant, which is something quite different. Aug 3, 2009 at 23:49
  • -1: erroneous, and even if it were not it simply reiterates the above two posts.
    – hobodave
    Aug 4, 2009 at 0:17
  • 2
    A constant is not a variable by definition (no pun intended) ;-) Aug 4, 2009 at 1:26
  • @hobodave ... yeah, except that it was postet 1 hour BEFORE the "above" two posts ... apart from that, i don't see, how this is erroneous ... in many languages constants are readonly variables ... apart from the fact, that a "constant" is semantically more clear, it is more less interchangable with "read-only variable" ...
    – back2dos
    Aug 4, 2009 at 2:28
0

A constant is very useful when you want access data from inside a function, check this

<?php
function data(){
  define("app","hey you can see me from outside the function",false);
  $tech = "xampp";
}
data();
echo $tech;
echo app;
?>

If you use a variable you are never going to get the inside value here is what i get

Notice: Undefined variable: tech in D:\xampp\htdocs\data\index.php on line 8 hey you can see me from outside the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.