Constants

Discussion in 'PHP' started by drewbe121212, Apr 18, 2006.

  1. #1
    How do you access constant values when using them in a statement?

    define('account_id','2');

    this does not work:

    if isset(account_id)

    nor does

    if (constant(account_id) < 2) yada yada

    Ideas or good tutorials for constants? The PHP website is more then unhelpful on this topic.
     
    drewbe121212, Apr 18, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    define('account_id','2');
    if (account_id == 2) {
      // account_id is 2
    }
    PHP:
    By convention, contants are usually uppercase
     
    exam, Apr 18, 2006 IP
  3. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #3
    If you don't care about the value, and just want to know if it's defined or not, use defined().
     
    digitalpoint, Apr 18, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    isset won't work as it's not a variable, it's a constant. As exam noted, all you have to do is use it like a variable or any other value.

    If you ever need to determine whether or not a constant is actually defined or not, you can call defined('CONSTANT_NAME')).
     
    TwistMyArm, Apr 18, 2006 IP
  5. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #5
    oh how interesting then! I have never used constantants this way, I am use to assigning them as a variable, ie String Const foo; From C# etc.

    Thank you very much for the help, and the capitalization is duely noted as well :)
     
    drewbe121212, Apr 18, 2006 IP
  6. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Also, I will assume (since I am using account_id as a constant), when the user is not logged in...

    define('ACCOUNT_ID',null);


    correct?
     
    drewbe121212, Apr 18, 2006 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    PHP doesn't use constants the same way C# does, for example. In C#, you modify a variable with the const modifier to say 'hey, this variable is not allowed to change'.

    In PHP, well, it works the same way, but you don't use them like that. PHP constants are more for what in C# you would use a #define or enum for. In PHP, constants are used to remove 'magic numbers' and so on.

    Technically, you could do what you're suggesting but in practice, you really, really shouldn't. It just not how things are done in PHP. In practice, you should just use variables as you do normally, but don't expect to be able to enforce their immutability.

    I hope that makes a bit of sense!
     
    TwistMyArm, Apr 18, 2006 IP
  8. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #8
    Yeah, my biggest want for the use of it is for holding something in a global scope, because of things being OOPS based. It's just a thing of me being lazy I guess, so that I dont spend 2 hours working on something, then realizing the only reason I can't get the variable to work is because I forgot to global it.
     
    drewbe121212, Apr 19, 2006 IP
  9. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You could use this for your account/authentication code, but you should probably read up on php sessions and just store the info in the super global $_SESSION array.
     
    exam, Apr 19, 2006 IP
  10. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #10
    I about about $_SESSION and have used it vastly. I am not sure what sparked this change, but for some reason I wanted to stay away from using sessions with this script. I think it is because I have been programming in vbulletin world to long, heh.

    Another reason I wanted to avoid the $_SESSION was the 15 minute timeout setting. It becomes rather annoying at times. :)
     
    drewbe121212, Apr 20, 2006 IP