Where do define a CONSTANT ?

Discussion in 'PHP' started by fatabbot, Jan 28, 2007.

  1. #1
    Since constant are global and accessible from any file once they are defined, i was wondering where to define them. People can access a site on many different pages...
    Does each page have to define all constants ? Or is it best to make one file constants.inc and include them on any page? Or what other options are there?
     
    fatabbot, Jan 28, 2007 IP
  2. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I have a 'constants.php' file and it is included in all my scripts. A few other php software packages I have seen also do this. I prefer to have them all defined in one file instead of doing it on each page.
     
    chopsticks, Jan 28, 2007 IP
  3. srinet

    srinet Peon

    Messages:
    172
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    any where you can define constants
     
    srinet, Jan 28, 2007 IP
  4. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you definitely want to define all your constants in a single script and include it in every other page
     
    picouli, Jan 28, 2007 IP
  5. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #5
    It is better to define them all in one file and then include that file into your project. This way you won't run into conflicts by forgetting that you have defined XYZ in three different files, etc.
     
    SilkySmooth, Jan 29, 2007 IP
  6. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #6
    in fact, it's best to group things as much as you can, eg all functions in one file, all classes in one files, etc, etc :)
     
    relixx, Jan 29, 2007 IP
  7. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can also do

    $bla = 'xyz';
    
    function my_function() {
       echo $GLOBALS['bla'];
    }
    
    my_function();
    PHP:
    That would echo 'xyz'.
     
    Icheb, Jan 29, 2007 IP