Beware - PHP newcomers !

Discussion in 'PHP' started by chriseccles2, Sep 30, 2008.

  1. #1
    If, like me, you come to PHP from a background in C, there is
    a big tripwire waiting to floor you !
    -
    If you declare a variable global in PHP, that
    variable will NOT be available to your functions.
    -
    This had me in knots for hours yesterday and I just hope this post
    can save any other newbies from tearing out their hair trying to
    figure what happens. You always have to pass values to functions in
    PHP as parameters.

    -
    Chris
     
    chriseccles2, Sep 30, 2008 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    lol nice post, i suffered the same fate a long time ago, makes me feel nostalgic :D
     
    serialCoder, Sep 30, 2008 IP
  3. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Interesting thing is, it seems to constrain one to write
    better code !
    -
    It makes some parameter lists a bit lengthy but, when you
    read through the script, the localisation is more clearly seen
    which, I guess, means lower maintenance three months down
    the line ........
    -
    :)

    -
    Chris
     
    chriseccles2, Oct 1, 2008 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    yep, global vars are not my thing anyways :)
     
    serialCoder, Oct 1, 2008 IP
  5. alvas

    alvas Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    global are meant to declared inside the function, not outside
     
    alvas, Oct 1, 2008 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    Yes, global is defined inside the function which allows the function to access the variable.
     
    Kaizoku, Oct 1, 2008 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Chris:

    As a newcomer, let me suggest to you that php.net is great for documentation... eg. http://php.net/global for how to access / use global variables.
     
    TwistMyArm, Oct 1, 2008 IP
  8. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Or you could use globals...

    
    $myvar = "hi";
    
    function printVar() {
      echo $GLOBALS["myvar"];
    }
    
    PHP:
     
    Steve136, Oct 1, 2008 IP
    dimitar christoff likes this.
  9. dwayne12

    dwayne12 Well-Known Member

    Messages:
    184
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Global variables are not really advisable. And if you do use them, don't declare them outside of a PHP function. Declare them INSIDE your PHP function.

    If you're going to want values that can be accessed site wide, but not changed, use constants.
    DEFINE('CONSTANT_NAME','CONSTANT_VALUE');

    That way they can be accessed site wide and don't have to be declared more than once. Just make sure you include a config file or something that contains your constants when needing them.
     
    dwayne12, Oct 1, 2008 IP
  10. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #10
    In PHP, that is ;)

    I really want to know the deal with global variables (using global $var inside your function). Everyone seems to hate it, but from what I can tell it does not slow the script down, and it is definitely not insecure. So if someone could enlighten me?
     
    blueparukia, Oct 2, 2008 IP
  11. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #11
    well, like it was said before - you can access any variable as $GLOBALS['varname'] anyway. i guess a local global $varname is simply performing an extract() on $GLOBALS for that key to make it easier to access... but it also means changes applied to the variable during the function will keep when it returns. i cannot comment on memory usage but you can do tests:

    function Bollocks() {
        echo memory_get_usage() . "<br />" . substr($GLOBALS['a'], 0, 20);
    }
    
    Bollocks();
    $a = str_repeat("Hello", 5000);
    Bollocks();
    PHP:
    this outputs memory usage for before and after $a is defined, when using the GLOBALS array:
    100200 // before
    125376 // after
    HelloHelloHelloHello

    when decalared as global $a:
        global $a;
        echo memory_get_usage() . "<br />" . substr($a, 0, 20);
    PHP:
    100416 // before is already higher by 216 bytes, just because...?
    125688 // 312 bytes more allocated.
    HelloHelloHelloHello

    interesting. hrm...
     
    dimitar christoff, Oct 2, 2008 IP
  12. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #12
    Cool, thanks.
     
    blueparukia, Oct 2, 2008 IP